son.of.the.morning Posted September 8, 2010 Share Posted September 8, 2010 I want to use javascript to open a confirmation box up before a record is deleted, but obviously when even you make a php page all the php code is executed first, any idears??? Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 8, 2010 Author Share Posted September 8, 2010 a litle help would be lovely... Quote Link to comment Share on other sites More sharing options...
Rifts Posted September 8, 2010 Share Posted September 8, 2010 try this it may work i have no idea <html> <head> <script type="text/javascript"> function confirmation() { var answer = confirm("Delete Record??") if (answer){ <?php *put delete code here* ?> } else{ alert("Did not delete") } } </script> </head> <body> <form> <input type="button" onclick="confirmation()" value="delete record"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 8, 2010 Author Share Posted September 8, 2010 didnt work at all, the php was exicuted before the javascript was. Quote Link to comment Share on other sites More sharing options...
Rifts Posted September 9, 2010 Share Posted September 9, 2010 ok i think i thought of a solution instead of if (answer){ <?php *put delete code here* ?> } replace that with javascript code to open a txt file and inside the txt file have the php Quote Link to comment Share on other sites More sharing options...
awjudd Posted September 9, 2010 Share Posted September 9, 2010 Assuming it just associated with a button ... you could do something like this ... <script type = 'text/javascript'> function checkDelete ( id ) { if ( confirm ( "Are you sure you want to delete " + id + "?" ) ) { // relocate to another page document.location.href = 'deleteurl.php?id=' + id; } else { alert ( "Stop the presses!" ); } } </script> Then in your actual page you would have something like: <input type = 'button' onclick = 'checkDelete("<?php echo $id; ?>")' /> ~juddster Quote Link to comment Share on other sites More sharing options...
DavidAM Posted September 9, 2010 Share Posted September 9, 2010 PHP is run on the server, before the page is sent to the user. Javascript is run in the browser. You cannot mix the two. Your PHP can write Javascript to be "added" to the page, but it cannot execute Javascript code. Javascript can issue a request to the server (AJAX) to execute a PHP script on the server and collect the results, but it cannot call PHP code directly. One way to accomplish your goal would be to use an OnSubmit handler for your form. As an example, I will take the code suggested by Rifts and make a few changes <html> <head> <script type="text/javascript"> function confirmation() { var answer = confirm("Delete Record??") if (answer){ return true; } else { return false; } } </script> </head> <body> <form action="/doDelete.php" method="POST" onSubmit="return confirmation()"> <input type="button" value="delete record"> </form> </body> </html> The form has to send a new request to the server to do the deletion. This will be a new script you will have to write. I've named it "doDelete.php", you can call it "fred.php" or "wilma.php" or whatever you want. But that script will have to do the delete. The "onSubmit" attribute of the FORM tag causes the specified function to be executed. This attribute has a "return" command in it, so that whatever the function returns is returned to the form handler (seems redundant to me too, but I didn't write the spec, and the didn't ask me). If the function "confirmation()" returns false, (and we return false to the submit handler) then the default action will not be performed. If we return true, then the default action will be performed. The default action is to send the form data to the script we specified in the action attribute of the form tag. The doDelete.php script will need to send a new page to the user. You can either show a confirmation, or redirect to the original page, or tell them good bye, whatever is appropriate. This functionality could also be accomplished using AJAX without having to refresh the page. But that will be more complicated. Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 9, 2010 Author Share Posted September 9, 2010 There is one issue though... I want to carry a var through to the delete page so unlink() knows what file i want deleting Quote Link to comment Share on other sites More sharing options...
DavidAM Posted September 9, 2010 Share Posted September 9, 2010 You can pass it as a hidden field on the form: <form action="/doDelete.php" method="POST" onSubmit="return confirmation()"> <input type="button" value="delete record"> <input type="hidden" name="delFile" value="filename.ext"> </form> Then retrieve the value from $_POST['delFile'] (or whatever name is assigned to the hidden field). Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted September 9, 2010 Author Share Posted September 9, 2010 I found a simple approach to this without using JavaScript... I passed the id to another page with has an image resembling a confirmation box (obviously this is a nice custom box) the image has to hotspots one being yes one being no. When yes is to be clicked i pass the var to the delete-image.php page, no will obviously return back to the table page. Quote Link to comment 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.