Page 1 of 1

Find Value of the submit button

Posted: Wed May 31, 2006 11:42 am
by reecec
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

Posted: Wed May 31, 2006 11:47 am
by feyd
look at the submission data, they're in there..

Posted: Wed May 31, 2006 11:47 am
by PrObLeM

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';
}
hope this helps

Posted: Wed May 31, 2006 11:48 am
by Oren

Code: Select all

if (isset($_POST['button_name']))
{
   // the button with the name 'button_name' was pressed
}

thanks

Posted: Wed May 31, 2006 11:59 am
by reecec
thanks 'PrObLeM' that is like what i want

Array ( [Button1_x] => 140 [Button1_y] => 12 [Button1] => Submit )

i just need the button name

please help



thanks reece

Posted: Wed May 31, 2006 2:54 pm
by ok
Look at this example:

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)
?>
This would print:
Submit!

Posted: Wed May 31, 2006 4:17 pm
by Christopher
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' />

Posted: Wed May 31, 2006 9:03 pm
by harrisonad
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

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>
this way, you only have to check the value of $_POST['submit_type'] to know which button are pressed.

Posted: Thu Jun 01, 2006 10:01 am
by pickle
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.