Re: 2 submit buttons.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2011-02-15 at 08:07 -0500, Floyd Resler wrote:
> On Feb 14, 2011, at 5:24 PM, Paul M Foster wrote:
> 
> > On Mon, Feb 14, 2011 at 05:15:11PM -0500, Floyd Resler wrote:
> > 
> >> 
> >> On Feb 14, 2011, at 4:18 PM, Paul M Foster wrote:
> >> 
> >>> On Mon, Feb 14, 2011 at 03:35:02PM -0400, Paul Halliday wrote:
> >>> 
> >>>> I have 2 buttons on a page:
> >>>> 
> >>>> if (isset($_POST['botton1'])) {dothing1();} if
> >>>> (isset($_POST['button2'])) {dothing2();}
> >>>> 
> >>>> They both work as intended when I click on them. If however I click
> >>>> within a text box and hit enter, they both fire.
> >>>> 
> >>>> Is there a way to stop this?
> >>> 
> >>> Check your code. My experience has been that forms with multiple
> >>> submits will fire the *first* submit in the form when you hit Enter
> >>> in a text field or whatever. I just tested this and found it to be
> >>> true.
> >>> 
> >>> Now, I'm doing this in Firefox on Linux. I suppose there could be
> >>> differences among browsers, but I suspect that the specs for HTML
> >>> mandate the behavior I describe.
> >>> 
> >>> Paul
> >>> 
> >> 
> >> If you don't mind using a little JavaScript you can test for which
> >> button should fire when enter is pressed.  How I would do it is to
> >> first add a hidden field and call it "buttonClicked".  Now, in the
> >> text field where you would like a button to fire if enter is pressed,
> >> at this to the tag: onkeyup="checkKey(this,event)".  For the
> >> JavaScript portion of it, do this:
> > 
> > Yeah, but you don't even have to go that far. Just put a print_r($_POST)
> > at the beginning of the file, and you'll see which button gets pressed.
> > It will show up in the POST array.
> > 
> > Paul
> > 
> > -- 
> 
> Yeah, except that the original question was about controlling which button fires when the enter key is pressed. :)
> 
> Thanks!
> Floyd
> 
> 

I think if you have more than 1 submit button, then you need to disable
the "enter to submit" functionality.  This would force people/users to
click on either of the submit buttons...

Or better yet, if you have a "default" submit button, have a hidden text
value with your default submit value, and then on the submit "onclick"
event of the other submit button, replace the text value of the hidden
field to something else.   Then finally in your postback check, check
the value of the hidden field to determine what you're going to do.

Personally, I do a combination of both.  I disable the ability to submit
on key press, and require you to submit via my submit methods, and
onclick of the submit button sets a value to the hidden text field, and
then do a switch() case:... on that hidden value.   But that is my way
of doing it (which will prolly get ripped apart by someone here, which
is good/constructive criticism for me)

steve


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux