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 is not deleting the record even after pressing ok.This is may be because the id is not passed properly. Can anyone tell why?Do I need to modify the javascript function?Here is my code:

function confirmation() {
	var answer = confirm("Delete Record?")
	if (!answer){
		
		window.location = "by_hand.php";
		}
         else
         {
               window.location = "delete.php?delete=$id&action=deleteemployee";
          }
}
<a href=# onclick='confirmation()'><img src='images/delete.png' alt='Delete' title='Delete' class='pngfix'></a>

Hi,

for what I can see, you don't have the id value, you just have the string '$id'.

This is all of your code?

Actually im displaying total records from the database and when user clicks on certain record, I want a confirmation box that user havent clicked the button accidentally.So $id is the id of that particular record.And through href the

delete.php?delete=$id&action=deleteemployee

link works fine.Just dont know how to do it through js.If I alert $id, it displays just $id and not the value.

You'll need to output your rows a little differently through PHP, add the id in the confirmation function:

<a href=# onclick='confirmation(1)'><img src='images/delete.png' alt='Delete' title='Delete' class='pngfix'></a>

And then use this javascript:

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

Thanks for your valuable reply pritaeas.It was very helpful and it solved the issue.
My new code is

<script type="text/javascript">
function deleteRecord(recID){
                var ans = window.confirm('Delete Record?');
                if(ans == true){
				                    window.location.href='delete.php?delete='+recID+'&action=deleteemployee';
                 }
            }
     </script>
$id=$row['eng_id'];
 echo '<a href="" onclick="deleteRecord('."'" . $id . "'" .');return false;"><img src="images/delete.png" alt="Delete" title="Delete" class="pngfix"></a>';
commented: Thanks for sharing. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.