Page 1 of 1
Determining which form submit button pressed
Posted: Sun Apr 29, 2007 6:24 pm
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?
Posted: Sun Apr 29, 2007 6:53 pm
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
Posted: Sun Apr 29, 2007 8:17 pm
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)
Posted: Mon Apr 30, 2007 7:35 am
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.
Posted: Mon Apr 30, 2007 8:35 am
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.
Posted: Mon Apr 30, 2007 9:00 am
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.
Posted: Mon Apr 30, 2007 9:06 am
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.
Posted: Mon Apr 30, 2007 12:44 pm
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
Posted: Mon Apr 30, 2007 1:48 pm
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.
Posted: Tue May 01, 2007 5:12 pm
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...?
Posted: Tue May 01, 2007 5:19 pm
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.
Posted: Tue May 01, 2007 9:30 pm
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.
Posted: Tue May 01, 2007 9:54 pm
by John Cartwright
Z3RO21 wrote:My theory is to treat all use data as if it was corrupt.
Yours and and good programmer..
Posted: Wed May 02, 2007 12:07 am
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')