Recently i found through the use of this a way of using buttons as images. i.e.
<input type="image" src="images/add.png">
<input type="hidden" name="add">
for HTML
and to process it (php):-
if (isset($_POST['add'])) {
}
this works if you have one button on the form. But if you have more than one it seems not to work. i.e if you add a delete button and have the hidden name as delete and process it using php. After trying that whatever button you press it runs both. Is there a way you can separate the two, so they will process when each individual button is pressed.
Also a second question-how can i make an image button act like a generic reset button and reset all the fields.
thanks.
more on images as form buttons
Moderator: General Moderators
-
microthick
- Forum Regular
- Posts: 543
- Joined: Wed Sep 24, 2003 2:15 pm
- Location: Vancouver, BC
Can you not add the Name attribute to image submit buttons?
Anyway, if you can't you can do it this way:
Basically this defines two submit buttons, one for submit and one for delete. When either is clicked, the JS function is called to change the value of the formaction form field.
Then in your PHP on the following page you can go:
Anyway, if you can't you can do it this way:
Code: Select all
<script language="JavaScript">
function setAction(myAction) {
document.myForm.formaction.value = myAction;
document.myForm.submit();
}
</script>
<form name="myForm" method="post" action="whatever.php">
<input type="hidden" name="formaction" value="update">
<input type="image" src="someimage.gif" onClick="setAction('update')">
<input type="image" src="anotherimage.gif" onClick="setAction('delete')">
</form>Then in your PHP on the following page you can go:
Code: Select all
<?php
if (isset($_POSTї"formaction"]) && $_POSTї"formaction"] != "") {
if ($_POSTї"formaction"] == "update") {
// do something
} else if ($_POSTї"formaction"] == "delete") {
// do something else
}
}
?>
Last edited by microthick on Mon Dec 15, 2003 9:41 am, edited 1 time in total.
- Bill H
- DevNet Resident
- Posts: 1136
- Joined: Sat Jun 01, 2002 10:16 am
- Location: San Diego CA
- Contact:
For the button:
And then to process it:
When the user clicks on an image, what is sent is the x and y coordinates of the point on the image at which he clicked. That is why the "Delete_x" in the isset() call.
Edit: this doesn't require Javascript, nor do you need hidden inputs.

Code: Select all
<input type=image src="images/ar_delete.gif" name="Delete">
<input type=image src="images/ar_process.gif" name="Process">Code: Select all
<?php
if (isset($_POST['Delete_x']))
{
// do whatever
}
elseif (isset($_POST['Process_x']))
{
// do whatever else
}
?>Edit: this doesn't require Javascript, nor do you need hidden inputs.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK