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 f开发者_StackOverflow中文版or 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
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
}
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
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.
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:
- An input name change affects both of the submit target pages, and requires changes on both of the
<form action="blah">
pages. - Javascript is not always enabled on browsers
精彩评论