Find Value of the submit button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Find Value of the submit button

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look at the submission data, they're in there..
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Code: Select all

if (isset($_POST['button_name']))
{
   // the button with the name 'button_name' was pressed
}
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

thanks

Post 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
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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' />
(#10850)
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply