seniramsu Posted April 4, 2010 Share Posted April 4, 2010 How can I use a javascript confirm pop-up box to tell my php code whether or not to execute? I'm setting up an admin page for a client to self-manage the photos in a photo gallery. Its similar to facebook's photo management system. I have a delete button within an html form, to be used for removing an html table element from the webpage (which is generated using php echo commands), as well as a table row within my database: //make a javascript popup that asks "are you sure you want to delete?" if(isset($_POST['delete_' . $id])) { $true = $_POST['delete_' . $id]; //begin javascript popup echo "<script language='javascript'>\n"; echo "function show_confirm() {\n"; echo "var r=confirm(\"Are you sure you want to delete this album?\")\n"; echo "if (r==true) {\n"; echo "alert(\"Album Deleted\")\n"; echo "} //if cancel is clicked, this php delete code will exit (else condition would go here?)\n"; echo "}</script>"; } //delete album if(isset($_POST['delete_' . $id])) { //actually deletes the information from the database $queryDelete = "DELETE FROM `ridgeline`.`albums` WHERE `albums`.`album_id` =" . $id . ""; $resultDelete = mysql_query($queryDelete, $db_server) or die("action failed." . mysql_error()); echo "album: \"" . $name . "\" has been deleted"; } I CAN remove the html element with my (other, not shown) php code, as well as remove the item from the database through the query. What I CAN'T do (yet) is be able to cancel out of the delete request using the Jscript popup 'cancel' button. As of now, if I hit delete in my form but click 'cancel' in the jscript popup, the delete request still carries through regardless. No bueno. Suggestions? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/197514-using-a-javascript-popup-to-control-a-php-function/ Share on other sites More sharing options...
seniramsu Posted April 4, 2010 Author Share Posted April 4, 2010 Additionally, My php echo commands work in calling my jscript popup box, and its functionality is good. (ie. displays the text I want it to and the buttons work). Quote Link to comment https://forums.phpfreaks.com/topic/197514-using-a-javascript-popup-to-control-a-php-function/#findComment-1036630 Share on other sites More sharing options...
jcbones Posted April 4, 2010 Share Posted April 4, 2010 Javascript is browser based, PHP is server based. You cannot call a PHP function with Javascript. However you can stop the active link with Javascript. <script type="text/javascript"> <!-- function confirmSubmit() { var agree=confirm("Are you sure you wish to delete?"); if (agree) return true ; else return false ; } //--> </script> <a onclick="return confirmSubmit();" href="?delete=<?php echo $id; ?>">Delete</a> Quote Link to comment https://forums.phpfreaks.com/topic/197514-using-a-javascript-popup-to-control-a-php-function/#findComment-1036640 Share on other sites More sharing options...
seniramsu Posted April 6, 2010 Author Share Posted April 6, 2010 For sure. I did find out that you can call javascript from within your php (in the body vice the head), by echoing each line of jscript code sequentially in php. (see my code above). It seems logical to me that if i were to echo a javascript function from within php (which works), I should be able to insert my own php variables into the javascript. Quote Link to comment https://forums.phpfreaks.com/topic/197514-using-a-javascript-popup-to-control-a-php-function/#findComment-1037951 Share on other sites More sharing options...
yanjchan Posted April 6, 2010 Share Posted April 6, 2010 If you want to insert the contents of PHP variables into server-side generated javascript, you could do something like this: echo "<script type=\"text/javascript\">"; echo "var foo = {$bar}"; echo "</script>"; I think that that should work. However, I'm by no means a Javascript or PHP whiz, so forgive me if that code is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/197514-using-a-javascript-popup-to-control-a-php-function/#findComment-1037957 Share on other sites More sharing options...
aeroswat Posted April 6, 2010 Share Posted April 6, 2010 You want to look into using AJAX. The following is an example of what you could do. First put your delete code in a separate php file which could be called deletestuff.php Then the ajax would be something like this function doSomething() { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","deletestuff.php",false); xmlhttp.send(null); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); xmlhttp.open("GET","deletestuff.php",false); xmlhttp.send(); } alert('That stuff has been deleted') } If you need something from the php file to be sent back to your javascript function then you could use responseText like so alert(xmlhttp.responseText); That will pop up a javascript alert box of whatever you echo'd in the php file Quote Link to comment https://forums.phpfreaks.com/topic/197514-using-a-javascript-popup-to-control-a-php-function/#findComment-1037961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.