A better way to do this is to add the message id to the button like so:
<input type="image" src="delete.png" name="delete;<?=$row['MessageId'];?>" />
This will give something like:
<input type="image" src="delete.png" name="delete;1" />
<input type="image" src="delete.png" name="delete;2" />
Now, the submitted data will only include the name of the used submit button. When you click the button with 'delete;1', print_r($_POST)
gives:
Array
(
[delete;1_x] => 25
[delete;1_y] => 9
)
If you click the image with 'delete;2', only 'delete;2' gets posted. That way you know which image was clicked and what to delete.
The thing with using images as a submit button is that you also get the coordinates of where you clicked on this image. But that's no problem, you can easily extract the message id:
// when submitted, go through all the submitted values
foreach ($_POST as $key => $value) {
// if a key starts with 'delete;', you know a delete image was clicked
if (substr($key,0,7)=='delete') {
// first remove the 'delete;' part
$key = str_replace('delete;','',$key);
// split the key on the '_' sign
$key = explode('_',$key);
/* now the $key variable is an array
Array
(
[0] => 1
[1] => x
)
*/
// and the first value is your message Id
$deleteId = $key[0];
// now validate that it's a number
if (preg_match('/^[0-9]+$/',$deleteId,$matches) {
// run your delete query
$sql = "DELETE FROM messages WHERE id = $deleteId";
mysql_query($sql);
}
// break the foreach loop, since you have what you need
break;
}
}
It's usually simpler when you don't use images, since you can simply split the posted value on e.g. ";" and you don't need to worry about the coordinates. But this is still a very simple way to retrieve the correct information and keep your HTML clean by only adding a simple id to the name of the image button.