Determining which form submit button pressed

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
benchmarkman
Forum Newbie
Posts: 6
Joined: Tue Apr 24, 2007 8:01 pm

Determining which form submit button pressed

Post by benchmarkman »

I have a page with an unknown amount of form submit buttons. Is there a way to determine which submit button was pressed?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

name each submit button differently or have a hidden field in each form with an identifier so you will know which form was submitted
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I would use a hidden input field, as the submit button is not always sent (e.g. the user hits enter instead of clicking the submit button)
benchmarkman
Forum Newbie
Posts: 6
Joined: Tue Apr 24, 2007 8:01 pm

Post by benchmarkman »

I got it working.
shiznatix wrote:name each submit button differently or have a hidden field in each form with an identifier so you will know which form was submitted
I had already done the different names I was not sure how to Identify which button had been pressed. I used a while loop and the isset method in the page I was submitting to.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Jcart wrote:I would use a hidden input field, as the submit button is not always sent (e.g. the user hits enter instead of clicking the submit button)
Clicking enter doesn't count as using the submit button? Are you sure? As far as I know, pressing enter in a form counts as using that particular form's submit button.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

superdezign wrote:Clicking enter doesn't count as using the submit button? Are you sure? As far as I know, pressing enter in a form counts as using that particular form's submit button.
It counts as, but may not send that particular button. It really depends on the browser's behaviors.
deleet
Forum Commoner
Posts: 28
Joined: Thu Mar 23, 2006 10:05 am

Post by deleet »

As far as I know, hitting Enter will be the same as clicking the next submit button (on the HTML code). So basically if your structure is something like:

<form field>
<submit1>
<submit2>

Enter will be the same as <submit1>.

Can't be absolutely sure this is the exact same behaviour for every browser though, some testing is recommended.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Ask and you shall receive.

Code: Select all

<?php

echo '<pre>';
print_r($_POST);
echo '</pre>';

?>

<form action="#" method="POST">
   <input type="text" name="foo1" value="bar1">
   <input type="submit" name="foo2" value="bar2">
</form>
Firefox 1.5

Code: Select all

Array
(
    [foo1] => bar1
    [foo2] => bar2
)
IE 7

Code: Select all

Array
(
    [foo1] => bar1
)
Last edited by John Cartwright on Mon Apr 30, 2007 1:49 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

IE does not send the button unless clicked. Opera and FF do. I know IE as of 5.5 (to 7) do not. FF as far back as the 1.X series did. Not sure about Opera. I am also not sure about Safari. Netscape may, depending on which identity it is using at the time of the submit.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

So, checking isset($_POST['this_forms_button']) isn't IE friendly? God... I was unaware.

I test a little in IE, but for the most part, I only test stylistic issues. I didn't know IE couldn't properly submit forms. So can you prevent someone from submitting one form to another page through altering the HTML? Is there any way to cope for some malicious user that really wants to mess around with stuff in the HTML?

Maybe JavaScript...?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

All you can do is validate. A user can easily gank the source of your form (View Source) and change whatever they want in it (like maxlength properties), strip out any Javascript validation and process it to your site without you knowing about it really.

PS Check the post array in IE when using an image submit button and clicking on it to submit. You'd like that a lot if you like the fact that it doesn't send the button when hitting enter in a form.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

I avoid using javascript for much validation. I use it to check length, and simple stuff like that but I always double check it server side. My theory is to treat all use data as if it was corrupt. I am glad I tend to use hidden fields to identify form elements.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Z3RO21 wrote:My theory is to treat all use data as if it was corrupt.
Yours and and good programmer..
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Way #1.
Make Different <form actions = ""> for different submit buttons
Way #2.
Give the Submit buttons Same value and then if($_POST['submit'] == 'form1')
Post Reply