Confirm box delets row even after pressing "Cancel"

Hi all.I found this code on a forum to confirm that the user wants to delete the record from database and that the link has not pressed accidently.But it delets the record even after pressing the cancel button.Can anyone tell why?Do I need to modify the javascript function?Here is my code:

<script type="text/javascript">
<!--
function confirmation() {
	var answer = confirm("Delete Record?")
	if (!answer){
		
		window.location = "by_hand.php";
		}
}
//-->
</script>

<a href=delete.php?delete=$id&&action=deleteemployee onclick='confirmation()'><img src='images/delete.png' alt='Delete' Title='Delete'class='pngfix'></a>

can’t it just be
function confirmation() {
if !(confirm(“Delete Record?”)) {
return false;
}
}
?

if the user hits OK, then confirm(“blah”) is True and the url you have should run.
if the user hits CANCEL, confirm(“blah”) is not true and we check for !confirm() and if so, return false, which stops browser action of the clicking.

Notes:


<script type="text/javascript">
[color=red]<!--[/color]
function confirmation() {
	var answer = confirm("Delete Record?")
	if (!answer){
		
		window.location = "by_hand.php";
		}
}
[color=red]//-->[/color]
</script>

Get rid of the stuff in red, you’re not writing for Netscape 4 or IE5 anymore.


<a href=[color=blue]'[/color]delete.php?delete=$id[color=blue]&amp;[/color][b][s]&&[/s][/b]action=deleteemployee[color=blue]'[/color] onclick='confirmation()'><img src='images/delete.png' alt='Delete' [s]T[/s][color=blue]t[/color]itle='Delete'[b]spacebar[/b]class='pngfix'></a>

Quote all your attribute values, escape your ampersands, keep spaces between your attributes, and remember that capitalisation matters (so generally lowercase everything in code).
I also would not put the title on the img. I’d probably have no title at all if you you are going to use one, put it on the anchor. The image already has an alt attribute.

Or you can the following JS code

function confirmation() {
	var answer = confirm("Delete Record?")
	if (!answer){
		
		window.location = "by_hand.php";
		}
         else
         {
               window.location = "delete.php?delete=$id&action=deleteemployee";
          }
}

and put “#” into href attribute.

Then you delete your record only when you answer OK. In your case you may call the link in href earlier than in window.location, so, the record will be deleted.

Thanks a lot for your replies Stomme poes and Alex69 :slight_smile:

I putted above code and it works fine now.It dosent delete the record.But it is not deleting the record even after pressing ok.This is may be because the id is not passed properly.As I am not too good with js, can you please tell me how to pass php variable through window.open or href?