Confirm before delete

Hi all

At present my script is showing a confirmation box but its deleting the item whether you select “OK” or “CANCEL”.

I want to delete the item only if “OK” is selected but it is deleting even if i select “CANCEL”

here is the php code

<?
if(isset($_REQUEST['id']))
{
$id=$_REQUEST['id'];
$qry="delete from user_table where user_id=$id";
$cd=$_REQUEST['category_id'];
    if(mysql_query($qry))
    {
    $msg="product Deleted Successfully";
}
    else
    {
    $msg="Error Deleting product";
    }
$url=$_REQUEST['url'];
header("Location:$url");
}
?>

here is the html

<script>
function ConfirmDelete()
{
  confirm("Are you sure you want to delete?");
}
</script>

<form name='del_update' action='manage.php?dealer_id=10&category_id=1' method='post'/>
<input type="submit" value="delete" Onclick="ConfirmDelete()" />
<input type="hidden" name="id" value="41" />
<input type="hidden" name="url" value="manage.php?dealer_id=10&category_id=1" />
</form>

thanks
vineet

You need to do something with the return value of your confirm(). Right now you just pop the window, and then the form submits, as the user clicked a submit button. The confirm() isn’t doing anything. It returns a true or false value, based on what they choose - you have to do some conditional logic here to make it worth anything.

Edit: Or even probably something like this?

<form name='del_update' action='manage.php?dealer_id=10&category_id=1' method='post' onsubmit='return confirm("Are you sure you want to delete?");' />

Removing your OnClick and the extra function?

Two changes.

First, return the confirm response from the function:

function ConfirmDelete() {
  return confirm("Are you sure you want to delete?");
}

Second, return that function response to the element:

<input type="submit" value="delete" Onclick="return ConfirmDelete()" />

As a small addendum, you might also want to separate HTML and JS like

document.getElementsByName('del_update')[0].onsubmit = function() {
    return confirm('Are you sure?');
};
1 Like

Thanks paul and jeffrey

its working fine now

vineet

Until someone clicks the ‘turn off JavaScript’ option that some browsers add to the confirm debugging call - or someone visits who has that debugging call turned off.

I’d recommend building your own confirm dialog rather than using the built in debugging call.

Hi felgall

as far as i know “Confirm” dialogs can only be created using javascript.

So whether i create my own dialog or use a inbuilt dialog, if the javascript if turned off, then they both will not work.

correct me if i m wrong ??

thanks
vineet

I just have the debugging dialogs turned off in my browser -JavaScript runs but any call to confirm just returns true without displaying a debugging message first.

Before I turned off confirm() I always used to select the “Stop Scripts executing on this page” option rather than the “ok” or "cancel alternatives as that was the first of the three alternatives confirm offered and if the page owner wanted me to turn off JavaScript by using a debugging call to ask me to then I of course did what their dialog asked me to.

I don’t have a copy of what that confirm looked like and can’t recreate one as it is off in all my browsers but here’s an example of an alert that I did a screen print off beforre I disabled the debugging dialogs. The ‘Stop Scripts executing on this page’ message was the same in all debugging dialogs.

For all these dialogs - alert, confirm, prompt - all of which are now off in my browser as there are better ways to debug now - I always selected the “Stop Scripts executing on this page” checkbox.

I am surprised more people don’t turn them off as they are not needed for debugging any more and that was all they were useful for after Netscape 4 died.

See http://limonte.github.io/sweetalert2/ for a simple script to create your own dialogs that don’t have debugging alternatives built in. The seventh option listed is a simple confirm box that doesn’t have a “Stop Scripts executing on this page” option and isn’t able to be turned off separately the way the debugging dialogs can be.

A short time ago in a galaxy very, very close…

If you don’t mind jQuery

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.