Problem with confirmation button

Hi,
I’ve a problem with easy confirmation script. It goes to delete.php either you hit the ok or cancel buttons.

<script type='text/javascript'>
<!--
	function confirmDelete() {
		return confirm("Are you sure you want to remove the user from database?");
	}
//-->
</script>
<?php
	echo "<a href='delete.php?user_id=" .$row['user_id']. "'><img src='images/delete.png' onclick='confirmDelete()' title='delete user' /></a>";
?>

Thanks for your help in advance

The returned value from the confirmDelete function needs to be returned to the onclick event.

When you use an inline attribute to assign a scripting event,


<... onclick='confirmDelete()'>

This is the function that gets assigned to that event:


function () {
    confirmDelete()
}

There are better ways to deal with that. For example, if you used scripting to assign confirmDelete to the event of the element, there is no outer function wrapper that needs to be dealt with, so the return from confirmDelete achieve exactly what needs to be achieved:


var deleteUser = document.getElementById('delete');
deleteUser.onclick = confirmDelete;

But a few other preparation pieces need to be performed before you can start doing things so easily.

So, in order for you to have the easiest solution (which may not be the best) you can just return to the event attribute the result of the confirmDelete function that you are calling.


<... onclick="return confirmDelete()">