0

When a user wants to delete a row from a database via my webpage, I want to prompt a message-box asking, "Are you sure you want to delete?". Then in my php code, I want to track if the user press Ok or Cancel and execute the delete operation as shown bellow. I am new to web-programming and any help would be appreciated.

<?php
.
.
.

if (user wants to delete a row from database) 
{
    echo "<script type=\"text/javascript\"> confirm(\"Are you sure you want to delete?\"); </script>";
    if(user press OK to delete")//How can I get input from the dialog box?
    {
        $deleterow = "DELETE FROM ElectronicShop WHERE WorkOrder = $i";
        $datadelete = sqlsrv_query($connectString, $deleterow) or die(print_r(sqlsrv_errors(SQLSRV_ERR_ALL), true));
        if($datadelete)
            echo "<br>you have deleted: $i Successfully";       
        else 
            echo "<br>could not be deleted: $i";
    }
    else
        echo "User pressed Cancel";
}

?>
D P.
  • 1,039
  • 7
  • 27
  • 56
  • 1
    Put the delete query code in another page, and when the user confirms (use `if (confirm("Press OK to delete."))`), then you can redirect them to the page with your delete code. – Dave Chen Jul 15 '14 at 21:02
  • http://blog.mashape.com/30-ways-to-make-rest-calls-in-node-js-php-python/ – Diodeus - James MacFarlane Jul 15 '14 at 21:03
  • http://programmers.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming – PeeHaa Jul 15 '14 at 21:04
  • Check out http://stackoverflow.com/questions/6445946/display-yes-and-no-buttons-instead-of-ok-and-cancel-in-confirm-box that post about yes/no dialogue boxes. – WASasquatch Jul 15 '14 at 21:05

1 Answers1

1

You can simply add the question to your onclick event

<a href="..." onclick="return confirm('are you sure?')">...</a>

or with jquery

<a href=".." class="delete-confirm">...</a>

$(".delete-confirm").click(function(e) {
    if (!confirm("are you sure"))
        e.preventDefault();
});
Philipp
  • 15,377
  • 4
  • 35
  • 52