Click to See Complete Forum and Search --> : JS Confirm with PHP


hdevr000
04-26-2003, 10:58 AM
Hi there.
I am using a database and php.
When I delete a record there must come a confirmation box with YEs and NO. On yes he goes thru the rest of the PHP code to delete. Choosing NO wil go one page back. This is what I have:



if ($action == "delete") {


print ("<script>
function ask() {
var agree=confirm('Are u sure?');
if (agree) {
alert('OK you were sure');
document.write('Deleted');
}
else {
alert('Record deleted');
history.go(-1);
}
}
ask();
</script> ");

ask();


The php code to delete...


When I push "cancel" it works well. If I choose OK, I get a php error saying "Undefined function". If I delete that last line Ask(); from the pgp source, then the Cancel Button also doesn;t work anymore. Can somebody help me?

AdamGundry
04-26-2003, 11:41 AM
You can't combine Javascript and PHP like this; PHP runs first, on the server, then Javascript runs afterwards and cannot control the output of the rest of the page.

You need to include the confirmation function on the link which causes the deletion to occur, like this:

<a href="delete.php" onclick="return confirm('Are you sure?')">Delete</a>

Adam

hdevr000
04-26-2003, 11:59 AM
OK, didn't know that!
Well I am also using :

<a href=\"$php_SELF?ID=$row[id]&action=delete\">
on that action so I think I can't combine that?

Thanks for your time,

HJ

AdamGundry
04-26-2003, 01:41 PM
You should still be able to use it, like this:
<a href=\"$php_SELF?ID=$row[id]&action=delete\" onclick=\"return confirm('Are you sure?')\">
Adam

hdevr000
04-26-2003, 02:12 PM
thank you very much. I was strugling much with this.

HJ