Search code examples
phpfunctionconfirmation

Confirmation Before Delete


I want to confirm from user before delete an entry

I am calling the delete function on click as

<li><img src="delete1.png" onClick="delete_records();" alt="delete_records" /></li>

the funcion is saved in a .js file as

function delete_records() 
{
document.frm.action = "delete.php";
document.frm.submit();
}

The delete.php is as follows

<?php require 'connections/connections.php';  ?>
<?php require 'includes/higherauth.php';// for user access level check  
?>

<?php 
// To stop accessing the page without login

if(isset($_SESSION["id"])){
}else{
header('Location: logIn.php');
}
?>
<?php

error_reporting(0); 
$rado = $_POST['rado'];
$chkcount = count($rado);

if(!isset($rado))
{
    ?>
    <script>
    alert('At least one checkbox Must be Selected !!!');
    window.location.href = 'account.php';
    </script>
    <?php
}
else
{
    for($i=0; $i<$chkcount; $i++)
    {

        $del = $rado[$i];
        $sql=$con->query("DELETE FROM mntr WHERE id=".$del);
    }   

    if($sql)
    {
        ?>
        <script>
        alert('<?php echo $chkcount; ?> Records Was Deleted !!!');
        window.location.href='account.php';
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
        alert('Error while Deleting , TRY AGAIN');
        window.location.href='account.php';
        </script>
        <?php
    }   
}
?>

Please help me to add a warning for confirming the delete


Solution

  • A simple way is add a confirm and test the response

    function delete_records() 
    {
        var conf= confirm("Do you really want delete?");
        if (conf== true){
           document.frm.action = "delete.php";
           document.frm.submit();
        }else{
          return;
        }
    }