Find Value of the submit button
Moderator: General Moderators
Find Value of the submit button
hi all
i have images as buttons that are used to submit to my php page
how do i get the php to find the name of the button that was presed
is it even posible
thanks reece
i have images as buttons that are used to submit to my php page
how do i get the php to find the name of the button that was presed
is it even posible
thanks reece
Code: Select all
<input type='image' src='URL TO IMAGE' name='foo' value='1'/>
<input type='image' src='URL TO IMAGE' name='bar' value='1'/>
<input type='image' src='URL TO IMAGE' name='baz' value='1'/>Code: Select all
if($_POST) {
print_r($_POST);
} else {
echo 'nope';
}Code: Select all
if (isset($_POST['button_name']))
{
// the button with the name 'button_name' was pressed
}Look at this example:
This would print:
Submit!
Code: Select all
...
<input type="submit" value="Submit!" name="mySubmit">
...Code: Select all
<?php
echo $_POST['mySubmit']; //You can use $_GET['mySubmit'] insteade (if u use the get method)
?>Submit!
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
I find that is it is a better practice to not depend on the submit button to check for the value -- a hidden input works better (and solves the IE press enter problem).
Code: Select all
<input type='hidden' name='submit' value='1'/>
<input type='image' src='URL TO IMAGE' name='foo' />(#10850)
- harrisonad
- Forum Contributor
- Posts: 288
- Joined: Fri Oct 15, 2004 4:58 am
- Location: Philippines
- Contact:
I dont think it's a good idea to check which button are pressed.
I suggest that you place a hidden input where upon clicking a button, changes its value.
for example
this way, you only have to check the value of $_POST['submit_type'] to know which button are pressed.
I suggest that you place a hidden input where upon clicking a button, changes its value.
for example
Code: Select all
<form name="frm" method="post">
<input type="hidden" name="submit_type" value="none">
<input type="image" src="image/buttons/submit_type_1.gif" onclick="frm.submit_type.value='type_1';submit()">
<input type="image" src="image/buttons/submit_type_3.gif" onclick="frm.submit_type.value='type_2';submit()">
<input type="image" src="image/buttons/submit_type_3.gif" onclick="frm.submit_type.value='type_3';submit()">
</form>When you use an image as a button, the exact name of the button isn't submitted, but it is included in the '_x' and '_y' properties sent. You could just iterate through $_POST and find any variables that end in '_x'. Take that variable name, strip off the '_x' and you'll get the button name.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.