I would like a way to have a confirm box pop up when a user clicks "delete" to remove a record.
Something like:
<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 an onclick:
echo "<input type='submit' name='submit' value='Delete' onclick='confirmDelete()' />";
But I know it's not possible to be using Javascript & PHP together like this. Is there another way to do this? Maybe PHP has its own kind of confirm? Please give any ideas!
Thank you.
I would like a way to have a confirm box pop up when a user clicks "delete" to remove a record.
Something like:
<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 an onclick:
echo "<input type='submit' name='submit' value='Delete' onclick='confirmDelete()' />";
But I know it's not possible to be using Javascript & PHP together like this. Is there another way to do this? Maybe PHP has its own kind of confirm? Please give any ideas!
Thank you.
Share Improve this question asked Dec 30, 2011 at 14:31 CatiaCatia 2693 gold badges5 silver badges11 bronze badges5 Answers
Reset to default 12It is pretty easy to make use of AJAX for this case. Simply place your PHP in the scriptDelete.php
file and make sure you pass the proper arguments and their values in the data
property. For this example I just pass an id of 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 would be to post the form back to its own page like:
<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 simply post 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());
}
?>
You should move the event to the form, and use onsubmit="confirmDelete()". If you return false within the ConfirmDelete function, the form submit is cancelled. If you return true, the form will get submitted like normal, and you can process it like a regular form on the server side.
<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 have regular processing code on the server side
The easiest way is to do a javascript function before you submit. If you click on false, the return will be false and it will not be submitted:
<form action="something.php" onSubmit="return confirm('Are you sure?');" method="post">
// Fields
// Fields
<input type="submit" name="Submit" class="button" value="Submit">
</form>
Easy way to confirm, instead of creating whole new function. Do this
<input type="submit" name="Delete" onClick='return confirm("Sure you want Delete this Page?")' />
this will exactly respond the same way.
Ajax. Place your php in a separate file, and use ajax to send it the id you want to use in your query. I recommend jQuery .post