tags:

views:

66

answers:

4

Would like to get a consensus as to what the best practice is in this scenario:

Muliple submit buttons, is it better to handle this by having separate FORMS for each one of the submits, OR is it okay to have one form and check which button was pressed?

thank you for your input :D

A: 

Well, depending on the content of the form, having 2 submit buttons would be the only way to achieve what you want. (How would you have 2 forms if there were text boxes.. replicating the entered data with javascript would be rather annoying)

I would stick with just checking to see which button was pushed, it should be much easier.

webdestroya
A: 

Of course, it always depends on the purpose. However, as a rule-of-thumb, I do the natural thing:

  • If there are form fields that would be submitted by either submit button, use Javascript and detect the submit button pressed with a handler.
  • In all other cases, use different forms.

I try to avoid having merged forms (the first option) as:

  1. An input name change affects both of the submit target pages, and requires changes on both of the <form action="blah"> pages.
  2. Javascript is not always enabled on browsers
phsource
+1  A: 

Assuming you're doing this solely in PHP, just change the values for each button and check for existence in POST:

if (isset($_POST['button1']) && $_POST['button1'] == "Previous") {

  // do something

} else if (isset($_POST['button1']) && $_POST['button1'] == "Next") {

  // do something else

} else {

  // nothing has been submitted, display default

}
Andrew Heath
A: 

In my case I would do that similar to Andrew Heath, but I woulg give the buttons a unique name. So the && clause is not needed

poeschlorn
That certainly works too, and is what I do in practice. I went with same names to emphasize the ability to have "one" button do several different things in the same form.
Andrew Heath