php_novice2007 Posted March 24, 2007 Share Posted March 24, 2007 Hi, I'm writing a page which has a form and I want a pop up box which confirms with the user before going to the next page... This is my code so far: <html> <head> <script type="text/javascript"> function confirm_delete(){ var r=confirm("Are you sure?"); // can i add in " you want to delete unit 100?" if (r==true){ // it automatically goes to delete_command.php, passing in the value chosen } else { window.location="testphpJava.php"; // doesn't work, goes to delete_command.php anyway :S } } </script> </head> <body> <b>Delete a unit: </b><br> <form name="myform" action="deleteCommand.php" method="POST"> Please select the unit number: <select name="unitNum"> <?php $unit_commands = array(); $unit_commands[0] = 1; $unit_commands[1] = 2; $unit_commands[2] = 10; for($j=0; $j < count($unit_commands); $j++) { // print $unit_commands[$j]; print "<option value=$unit_commands[$j]>$unit_commands[$j]</option>"; } ?> </select><br> <input type="Submit" name="a" action="delete_command.php" value="Delete Command" onclick="confirm_delete();"> <br></form></body></html> The bit I'm most concerned with is that even if the user clicks cancel, delete_command.php is still loaded. How do I stop it so that it just stays at the current page? Also (not essential but it would make the site better...), would it be possible to somehow pass the unit number chosen to the javascript, so that instead of just asking "Are you sure?" it can ask "Are you sure you want to delete unit 2?" I really need to figure out the first bit, can't find anything on the web.. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/44108-php-and-javascript/ Share on other sites More sharing options...
DanDaBeginner Posted March 24, 2007 Share Posted March 24, 2007 try this it works for me: window.location.href="testphpJava.php" ----------- about the delete try this: function delete() { var c = confirm("Are you sure you want to delete?"); if (c==true) return true; } so in your form it would be like this: <form name="myform" action="deleteCommand.php" method="POST" onsubmit="delete(); return false"> Link to comment https://forums.phpfreaks.com/topic/44108-php-and-javascript/#findComment-214157 Share on other sites More sharing options...
php_novice2007 Posted March 24, 2007 Author Share Posted March 24, 2007 function _delete() { var c = confirm("Are you sure you want to delete?"); if (c==true) return true; } </script> </head> <body> <b>Delete a unit: </b><br> <form name="myform" action="deleteCommand.php" method="POST" onsubmit="_delete(); return false"> Please select the unit number: <select name="unitNum"> <?php $unit_commands = array(); $unit_commands[0] = 1; $unit_commands[1] = 2; $unit_commands[2] = 10; for($j=0; $j < count($unit_commands); $j++) { // print $unit_commands[$j]; print "<option value=$unit_commands[$j]>$unit_commands[$j]</option>"; } ?> </select><br> <input type="Submit" name="a" value="Delete Command"> <br></form></body></html> Now it just stays on the same page whether I click ok or cancel... I also tried the other way of using window.location.href="testphpJava.php" instead of just window.location in the code from my first post, my page still went to deleteCommand.php... Link to comment https://forums.phpfreaks.com/topic/44108-php-and-javascript/#findComment-214160 Share on other sites More sharing options...
DanDaBeginner Posted March 24, 2007 Share Posted March 24, 2007 what browser are you using? does your browser have error console? can you check whats the error in the javascript? is there undefinded variable or something? because the code I gave you works perfect to me.. Link to comment https://forums.phpfreaks.com/topic/44108-php-and-javascript/#findComment-214165 Share on other sites More sharing options...
php_novice2007 Posted March 24, 2007 Author Share Posted March 24, 2007 Hi, my browser is IE7. How do I get error console.? There didn't seem to be any javascript error.. When I click on Delete Command, it just takes me to deleteCommand.php straight away, there was no confirmation box at all.. I copied your code into my code and the full code is: <html> <head> <script type="text/javascript"> function delete() { var c = confirm("Are you sure you want to delete?"); if (c==true) return true; } </script> </head> <body> <b>Delete a unit: </b><br> <form name="myform" action="deleteCommand.php" method="POST" onsubmit="delete(); return false"> Please select the unit number: <select name="unitNum"> <?php $unit_commands = array(); $unit_commands[0] = 1; $unit_commands[1] = 2; $unit_commands[2] = 10; for($j=0; $j < count($unit_commands); $j++) { print "<option value=$unit_commands[$j]>$unit_commands[$j]</option>"; } ?> </select><br> <input type="Submit" name="a" value="Delete Command"> <br></form></body></html> weird.. :S Link to comment https://forums.phpfreaks.com/topic/44108-php-and-javascript/#findComment-214481 Share on other sites More sharing options...
suzzane2020 Posted March 24, 2007 Share Posted March 24, 2007 Call this java script on the link for delete.Pass the relevant id's as the parameters eg: if the user clicks cancel the the link wud be the same page $p=same page if the user clicks yes, the page wud be delete.php $d=delete.php function Alert(d,p) {varp=p; var d=d; var where_to= confirm("Do you really want to delete this job posting??"); if (where_to== true) { window.location=d; } else { window.location=p; } } Link to comment https://forums.phpfreaks.com/topic/44108-php-and-javascript/#findComment-214506 Share on other sites More sharing options...
php_novice2007 Posted March 24, 2007 Author Share Posted March 24, 2007 oh thank you It works now. I did changed your code slightly though.. oh and its a little weird because the javascript is not immediately executed even though i put it in the head section of the code.. I had to call it in the body: testphpJava.php <html> <body> <b>Delete a unit: </b><br> <form name="myform" action="deleteCommand.php" method="post"> Please select the unit number: <select name="unitNum"> <?php $unit_commands = array(); $unit_commands[0] = 1; $unit_commands[1] = 2; $unit_commands[2] = 10; for($j=0; $j < count($unit_commands); $j++) { // print $unit_commands[$j]; print "<option value=$unit_commands[$j]>$unit_commands[$j]</option>"; } ?> </select><br> <input type="Submit" name="a" value="Delete Command"> <br></form></body></html> deleteCommand.php <html> <head> <script type="text/javascript"> function alert() { //var p=p; //var d=d; var where_to= confirm("Do you really want to delete this job posting??"); if (where_to== true) { //window.location="deleteCommand.php"; } else { window.location="testphpJava.php"; } } </script> </head> <body> <script> alert();</script> <?php if (isset($_POST['unitNum'])) { echo $_POST['unitNum']; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/44108-php-and-javascript/#findComment-214522 Share on other sites More sharing options...
suzzane2020 Posted March 25, 2007 Share Posted March 25, 2007 You cud call the function on the form submit event in the first page.So the alert wud pop up before goin to the delete command page Link to comment https://forums.phpfreaks.com/topic/44108-php-and-javascript/#findComment-214531 Share on other sites More sharing options...
php_novice2007 Posted March 25, 2007 Author Share Posted March 25, 2007 damn it doesn't work... even if the user clicks cancel, because its php code thats doing the deleting from the database, it is executed anyway... How can I stop the php code from executingif the user clicks cancel, ahhh Link to comment https://forums.phpfreaks.com/topic/44108-php-and-javascript/#findComment-214546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.