Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

confirm box

Status
Not open for further replies.

patrickstrijdonck

Programmer
Jan 18, 2009
97
NL
Hi there,

I want to have a confirm box, asking (confirming) the user if its alright to delete.

what i have now is this:

Code:
if ($_POST['DEL'] == 'Personeelslid Verwijderen')
{ 			
?>
<script type="text/javascript"> 
if (window.confirm("This will change data in the database. 
Do you want to go ahead?")) { 
<? $persnr = $_SESSION['pznummer'];
$query1="DELETE FROM personeel WHERE Personeelsnummer = '$persnr'";
mysql_query($query1) or die(mysql_error());
header("Location: update.php"); ?> 
} else { 
// Don't change the data... 
} 

</script> 
<?
}

But when i click the button, it will just delete the record without asking,

Am i missing something?
 
PHP cannot directly interact with Javascript.

PHP runs on the server while Js runs on the client. So your Javascript confirmation will not be shown until PHP has done its thing. Which means the object will have already been deleted.
and if you have a redirect at some point, then the alert won't even be shown.

If you want a confirmation like that, then you'll need to use it on the form before it submits to the PHP script.

Something like:
Code:
<form action="process.php" method=POST onSubmit="window.confirm(...)">


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
function checkAction(myMessage)
{
result=confirm(myMessage);
if (result==true) { return true; } else { return false; }
}


<input class="form_button" name="" type="submit" value="Del" onClick="return checkAction(\'Delete this page?\')" />


If you click cancel it will do nothing, if you click confirm (or whatever the other one is) it will submit the form...

------------------------
 
Thanks JamesGMills for your reaction,

what i have now is:
Code:
?>
<SCRIPT LANGUAGE="Javascript">
<!--		
			function checkAction(myMessage)
			{    result=confirm(myMessage);   
			if (result==true) { return true; } else { return false; }
			}
			//-->
</SCRIPT>
and as button:
Code:
      <input class="form_button" name="DEL" type="submit" value="Personeelslid Verwijderen" onClick="return checkAction(\'Weet u zeker dat u deze personeelslid wilt verwijderen?\')" />

but it still isnt asking the question....

Am i doing something wrong, or is just the code you gave me not Javascript???

Ty in advance
 
you should ask your question in the javascript forum.
 
While i agree this is a JS problem now, the error seems to stem from your escaping your single quotes.

remove the backslashes and you should be o.k.

Code:
<input class="form_button" name="DEL" type="submit" value="Personeelslid Verwijderen" onClick="return checkAction([red]\[/red]'Weet u zeker dat u deze personeelslid wilt verwijderen?[red]\[/red]')" />




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
lol,

Posted it on Javascript forum, and yes it was the \ that needed to remove,

It works now, thanks for the help
 
remember to secure the variable (sql injection), also limit it 0,1.

Olav Alexander Mjelde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top