OK, this has been driving me crazy all afternoon, to the point I thought I’d better ask for help. On my site a user has a list of items in their collection, and next to each I want to have a small image that triggers the delete process for that particular item. When the user clicks the button, they get a pop-up confirmation box, then if they hit OK it goes back to that page with the item now deleted…or that’s how it should work.
On the top of the page, it checks to see if $_POST[‘delete’] is set:
[code]If (isset($_POST[‘delete’])) {
mysql_query("DELETE FROM tCollection WHERE ID = " . $_POST['delete'] . "");
echo '<script type="text/javascript">
alert("Item has been removed from your collection.");
</script>';
}
[/code]
Here’s what I use for the image itself (this is part of a larger “echo” statement in PHP):
<form name="deleteform" method="post" action="' . $_SERVER['PHP_SELF'] . '">
<input type="hidden" name="delete">
<a onclick="confirmdelete()" href="javascript:deleteitem(\'' . $row['c_id'] . '\')">
<img src="images/delete.jpg">
</a></form>
Then there are the two Javascript bits for the pop-up box and in turn the action of using the image as a submit button:
//Script to delete from collection
echo '<script type="text/javascript">
<!--
function confirmdelete() {
var answer = confirm("Delete this item from your collection?")
if (answer)
return true;
else
return false;
exit;
}
//-->
</script>';
echo '<script language="JavaScript" type="text/javascript">
<!--
function deleteitem ( itemnum )
{
document.deleteform.delete.value = itemnum ;
document.deleteform.submit() ;
}
-->
</script>';
Despite messing with this all day I can’t get the delete.jpg image to trigger the submission of the form. I get the pop-up asking if I really want to delete the item, but from there it just stays on the same page, doesn’t reload and doesn’t execute the delete query. The kicker - I’ve copied the necessary code for this process into a test PHP file and it works just fine. I also have this set up on another page (with different names for the form and form JS code), and it works just fine as well.
If anyone can offer any insight into my problem, I’d be eternally grateful!