Jump to content

Running PHP inside js dialog box.


coupe-r

Recommended Posts

I am trying to display a js dialog box when someone wants to delete something.  Currently I have it all with buttons and PHP in the background.  When they click the delete icon, I want a dialog box to appear with OK or Cancel.  On cancel, return back to the same page, If OK, I want it to run the PHP/mysql code that deletes the record from the DB.  Once deletes, I want the box to appear again saying it was deleted successfully.

 

I found this but now sure how to make it work.

 

function show_confirm()

{

var r=confirm("Press a button");

if (r==true)

  {

  alert("You pressed OK!");

  }

else

  {

  alert("You pressed Cancel!");

  }

}

Link to comment
Share on other sites

The code that you have there is to prompt a user for an answer. If you wanted you could bind that the onclick event of an element on the page. However, if you want to display a dialog when it is done that is a little more confusing as you will have to do a page refresh and then have that javascript popup open on page reload. There is a more elegant solution that I could suggest. The technology that you need to look into is ajax. It allows you to communicate with php via javascript in a sense. One way people use ajax is with ajaxobject (just google it) and another way is with jQuery. If you are a beginner and would like to play around with using php and javascript in combination I would look into jquery as it is really easy to get a graspe on. If you do decide to look into jQuery (http://jquery.com) you should take a look at the $.ajax() method or one of my personal favorites the $.load() method. Post back when you have more questions and I would be more than willing to help.

Link to comment
Share on other sites

Personally I like jQuery because it has some really nice built in features that make javascript really easy and fast to use. However, for this I would use a function similiar to what you have. So first thing I would do is change your submit button to just a regular button and then add the javascript call to it passing the url for the delete.

 

<input type="button" onClick="delete('index.php?delete=true&id=5');" />

 

Then you can setup the javascript function just add this somewhere in the page.

 

<script type="text/javascript">
function delete(url) {
var answer = confirm("Are you sure you would like to delete this item?")
if (answer){
	window.location = url;
}
else{
	return false;
}
}
</script>

 

 

Please ask if you have any questions.

Link to comment
Share on other sites

Well, what I have is a .png icon and when you click it, the URL changes to del=true?id=123, instead of an actual delete button.  Here is what I have

 

"<a href='index.php?id=".$id."&del=1'>".'<img src="../images/delete_sm.png"  title="Delete Application" alt="Delete Application" width="20" height="20" border="0" />'

Link to comment
Share on other sites

Same concept just a different method. If my assumption is correct this is coming from an echo statement. Try this:

 

 

'<a onclick="delete(\'index.php?id='.$id.'&del=1\');"><img src="../images/delete_sm.png"  title="Delete Application" alt="Delete Application" width="20" height="20" border="0" />'

 

 

Then somewhere on that page add that javascript code that I previously posted outside of the php tags.

Link to comment
Share on other sites

Ok so I figured it out. It was my fault, apparently delete isnt a valid name for a function because it is already used I am guessing lol. Also you have to add the href attribute and just set it to javascript:void(0); (means do nothing). change it to this:

 

 

$del_text = '<a onclick="deleteApplication(\'index.php?id='.$id.'&del=1\');">';

 

 

Then change the javascript functions name to deleteApplication instead of delete.

Link to comment
Share on other sites

Alright, now we're getting somewhere.  OK, now when the dialog box pop up and I hit OK, it takes me to index.php?id=98&del=1.

 

Is there a way, instead of taking me to a URL, that it executes the delete query?

 

Or is my only option to have an

 

if(isset($_GET['id']) && isset($_GET['del']))

{

DELETE CODE HERE;

}

Link to comment
Share on other sites

Like I was saying before the only way to do that is by using ajax to send the request to the page without using a reload. However, If you are just looking for the user to return the the same page you could just use a redirect. For example:

 

 

<?php//delete querymysql_query("DELETE something FROM something WHERE somthing='something'");//redirectheader("Location:index.php");?>

 

 

All this does is it allows you to delete the entry but then redirect them to the same page so they dont notice where they went when they got redirected. Obviously you would have to change the location to the correct page. Another thing to keep is that you would want this code at the very top of the page because the header function can only be used before anything is out put is sent to the page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.