0

Dear Sir,

I have following codes

if(isset($_POST['delete']))
    {
        // Get values from form 
        $sno = $_POST['txtsno'];
        $pro = $_POST['txtpro'];
        $wet = $_POST['txtwet'];

$query = "delete from test WHERE sno={$sno}" ;
$result = mysqli_query($con, $query);

if(!$result){
echo "<script>alert('Record not deleted')</script>";
}else{
echo ("<script>alert('Record deleted successfully')</script>"); 
}
}

It works fine, but I want to need a confirmation box like this before deleting record

[IMG]http://i42.tinypic.com/15mzc6g.jpg[/IMG]

Please help

4
Contributors
8
Replies
39
Views
3 Years
Discussion Span
Last Post by OsaMasw
2

I would suggest that you show the popup message prior to the post. You can achieve this result using javascript and/or jquery. It can be as simple as using the confim method on your button... onclick="return confirm('Are you sure you want to delete the record?');

0

Thanks sir, but we should add your codes in my code?
Please modify my codes if possible.

0

Your code is not complete. I assume that you have a form somewhere that is handing the submission process? If you have a submit button that is there for the user to initiate the deletion process, then...

<form action="deleteRecord.php" >

<input name="deleteBtn" type="submit" value="Delete" 
       onclick="return confirm('Are you sure you want to delete the record?')"/>

</form>

Edited by JorgeM

0

Thanks for helping, now I have following modified codes

<?php
    require_once("connect.php");

    // DELETE
    if(isset($_POST['delete']))
    {
        // Get values from form 
        $sno = $_POST['txtsno'];
        $pro = $_POST['txtpro'];
        $wet = $_POST['txtwet'];

            //$query = "delete from test WHERE sno='".$sno."'" ;
            $query = "delete from ghee WHERE sno={$sno}" ;
            $result = mysqli_query($con, $query);

            if(!$result)            {
            echo "<script>alert('Record not deleted')</script>";
            }else{
            echo ("<script>alert('Record deleted successfully')</script>"); 
                  }
        }
?>

      <form name="form1" action="" method="post">
        <table border=0; cellpadding="1" cellspacing="1" bgcolor="" align="center" >
        >
        <div style=text-align:center;margin-top:20px;>

          <input type="submit" name="delete" value="Delete" onclick="return (window.confirm('Do you really want to delete this record?') ? 1 : 0);"
>

        </div>
      </form>

When I press delete button then confirmation apperas but..
In both cases (Yes or No), record is deleted automaticlly.

I think when Delete button is pressed then this part of codes runs immediately without caring Yes or No option.

if(isset($_POST['delete']))   

The record must not be deleted in case NO button is pressed.

Please help again. Thanks

0

Hai;

Try this

<script src="http://code.jquery.com/jquery-1.8.1.js"></script>
<?php
    //require_once("connect.php");

    // DELETE
    if(isset($_POST['delete']))
    {
        echo '<h1>Posted Value ='.$_POST['delete'].'</h1>';

     }
?>

      <form name="form1" id="formId" action="" method="post">
       <input name="delete" type="hidden" value="Deleted"  />
        <table border=0; cellpadding="1" cellspacing="1" bgcolor="" align="center" >

        <div style=text-align:center;margin-top:20px;>

      <input type="button" name="delete" value="Delete" onclick="formConfirm()">

        </div>
        </table>
      </form>
<script type="text/javascript">

    function formConfirm() {
        if(!confirm("Do you really want to delete this record?")) return false;
        $("#formId").submit();
    }

</script>

Edited by Bachu: text correction

0

Dear Sir, can please provide some more explation about following funciont?

 function formConfirm() {
        if(!confirm("Do you really want to delete this record?")) return false;
        $("#formId").submit();
    }
0
onclick="return confirm('Are you sure you want to delete the record?');"

I use this in my projects.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.