Ok, so let's assume the following (forgive me for re-clarifying the question):
You have a number of rows of some form, with delete links, and you want to confirm that the user actually wants to delete it?
Let's assume the following html:
<tr>
<td>Some Item 1</td>
<td><a href="?mode=delete&id=1" class="delete-link">Delete</a></td>
</tr>
<tr>
<td>Some Item 2</td>
<td><a href="?mode=delete&id=2" class="delete-link">Delete</a></td>
</tr>
<tr>
<td>Some Item 3</td>
<td><a href="?mode=delete&id=3" class="delete-link">Delete</a></td>
</tr>
So I'm assuming the same php script can run the delete, picking up on the mode parameter:
<?php
if($_GET['mode'] == 'delete') {
//check there is something in $_GET['id']
if($_GET['id']) {
//prevent sql injection, just to be safe
$query = "DELETE FROM sometable WHERE id='" . mysql_real_escape_string($_GET['id']) . "'";
mysql_query($query);
}
}
I'm going to give two solutions to this on the javascript side - the first with an inline, slightly ugly solution, the second using jQuery (http://jquery.com/), and unobtrusive javascript.
Ok, so for the first, I would bind on the onclick event of each link.
<tr>
<td>Some Item 3</td>
<td><a href="?mode=delete&id=3" class="delete-link" onclick="checkDeleteItem();">Delete</a></td>
</tr>
Then create a javascript function:
//this will get called when the link is clicked
function checkDeleteItem() {
//show the confirmation box
return confirm('Are you sure you want to delete this?');
}
As I said, I don't like that solution, because it is horribly obtrusive, and also not particularly robust.
Now, the jQuery solution:
//do all this when the dom is loaded
$(function() {
//get all delete links (note the class i gave them in the html)
$("a.delete-link").click(function() {
//basically, if confirm is true (ok button is pressed), then
//the click event is permitted to continue, and the link will
//be followed - however, if the cancel is pressed, the click event will be stopped here
return confirm("Are you sure you want to delete this?");
});
});
I heartily recommend this solution over the previous one, since it is much more elegant and nice, and is generally best practice.