Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript confirm with PHP function afterward

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.

like image 584
Catia Avatar asked Dec 30 '11 14:12

Catia


3 Answers

It 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());
   }
?>
like image 72
Jules Avatar answered Sep 22 '22 07:09

Jules


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

like image 26
Mitch Grande Avatar answered Sep 23 '22 07:09

Mitch Grande


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>
like image 20
Xuntar Avatar answered Sep 26 '22 07:09

Xuntar