After that, Javascript will confirm the PHP function.

I would like the confirmation window to appear when the user clicks β€œdelete” to delete the entry.

Sort of:

<script language="JavaScript" type="text/javascript" > function confirmDelete() { if(confirm("Would you like to delete the selected products?")) { <?php $allCheckBoxId = $_POST['checkbox']; array_map ('mysql_real_escape_string', $allCheckBoxId); $ids = implode(",", $allCheckBoxId); $sql = "DELETE FROM products WHERE `id` IN ($ids)"; mysql_query($sql); ?> } } </script> 

With onclick:

 echo "<input type='submit' name='submit' value='Delete' onclick='confirmDelete()' />"; 

But I know that it is impossible to use Javascript and PHP together like that. Is there any other way to do this? Maybe PHP has its own kind of confirmation? Please give any ideas!

Thanks.

+4
source share
5 answers

Easy enough to use AJAX for this case. Just put your PHP in the scriptDelete.php file and make sure you pass the correct arguments and their values ​​in the data property. In this example, I just pass id 5 .

 <script type="text/javascript"> function confirmDelete() { if (confirm('Are you sure you want to delete this?')) { //Make ajax call $.ajax({ url: "scriptDelete.php", type: "POST", data: {id : 5}, dataType: "html", success: function() { alert("It was succesfully deleted!"); } }); } } </script> <input type='submit' name='submit' value='Delete' onclick='return confirmDelete()' /> 

Another way is to submit the form back to your own page, for example:

 <form method="post" action="yourpage.php"> <input type="submit" name="submit" value="delete" onclick="return confirm('Are you sure you want to delete this?')" /> </form> 

So, you just submit the form back to yourpage.php , and on top of this page you do something like:

 <?php //Means the form was submitted if ($_POST['submit']) { $id = $_POST['idToDelete']; $query = "DELETE FROM products WHERE id = " . mysql_real_escape_string($id); mysql_query($query) or die(mysql_error()); } ?> 
+11
source

You must transfer the event to the form and use onsubmit = "confirmDelete ()". If you return false in the ConfirmDelete function, the form submission is canceled. If you return true, the form will be submitted as normal, and you can treat it as a normal server-side form.

 <script type="text/javascript"> function confirmDelete() { return window.confirm("Are you sure you want to delete this record?"); } </script> <form action="myDelete.php" onsubmit="confirmDelete()"> ....inputs <input type="submit" name="submit" value="Delete"> </form> 

Then the server will have the usual processing code

+2
source

The easiest way is to execute the javascript function before submitting. If you click on false, the return will be false and it will not be sent:

 <form action="something.php" onSubmit="return confirm('Are you sure?');" method="post"> // Fields // Fields <input type="submit" name="Submit" class="button" value="Submit"> </form> 
+1
source

A simple way to confirm, rather than create a whole new function. Do it

 <input type="submit" name="Delete" onClick='return confirm("Sure you want Delete this Page?")' /> 

it will answer exactly the same way.

+1
source

Ajax. Put your php in a separate file and use ajax to send the identifier you want to use in your request. I recommend jQuery.post

0
source

Source: https://habr.com/ru/post/1388619/


All Articles