PHP Submit Button

I see a lot of sites having two submit buttons on one page, like for a search input button and a login input button on the same page. Naming them both using name=“submit” causes problems for me, however, other sites seem to use it. Won’t there be a conflict if you use the same name for two different submit buttons if you plan to be using it in the $_POST array? Is the only solution to name the two items differently?

In addition, some buttons don’t even have name=“submit”. What’s the point of them if you can’t reach them using PHP with $_POST[‘submit’]?

Appreciate any help, thanks!

You can test the value to see which button was clicked.

Putting a name on a submit button is generally useless unless you actually have multiple submit buttons. A submit button is a bad way to test if a form was submitted. Some browsers will not send the name=value pair of the submit button if the form was not submitted with the submit button. For example, using the enter key to submit a form.

There’s lots of ways to test for form submission via post.


if ($_SERVER['REQUEST_METHOD'] === 'POST'){}

if ($_POST){}

The latter method requires that the browser sends at least 1 name to the server. This matters in case you might have a form which contains nothing but optional form elements, like checkboxes. The user might submit with none checked. Testing the request method will allow you to still recognize the form post. You might not want to accept it either way, but that’s a different topic.

Btw- because some browsers don’t send the name of a submit button if the form was submitted by hitting enter on an input field, you should plan for this. If you have a form that does have multiple submit buttons(and you need them), if you cannot detect any of the submit button names, you should assume the user submitted via enter, and assume the action of the first submit button, because thats the button browser will send, if they do send it.

Wow I never knew that, that $_POST[’{submitName}'] doesn't always work. I'll start using _POST from now on when I only have one POST request item on a page.

If you assume the action of the first submit button, and you’re assuming the browser doesn’t read the names that are sent with the submit request, how do you apply actions to the second submit button? Or is it generally better to have only one per page?

Using multiple submit buttons is ok, if they make sense. Sometimes it makes more sense to use a single button, but provide a checkbox, or a radio button or something else to distinguish between multiple things.

The only way to detect the second(or third+) submit buttons is if the browser actually sends the name, and that requires the user to either click it with the mouse, or tab to that specific button to give it focus, and then hit enter on that button.

The detection logic for a form with 3 buttons would be


// if there was a form post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['submit3'])) {
        //button 3 was activated
    } elseif (isset($_POST['submit2'])) {
        //button 2 was activated
    } else {
        // we can assume button 1 was activated. 
        // no real need to test because we know 
        // the form was submitted, and it wasn't via the other 2 buttons
        // so this is the only remaining possibility
    }
}

I generally like to use a different name for each submit, if I’m using multiple buttons. I don’t bother giving the first button a name. I don’t use the value of the button anyway, rather, I just treat the buttons as flags.

There’s always different ways to do it, and some make more sense in different situations. Just, be aware of the possibility of not getting any submit button name sent, and how to handle it.

Thank you very much, appreciate the help a lot!

All these answers are all right for the single form
But in the first post were 2 forms described. Search form and login form. So, no conflicts at all.

For the single form in shouldn’t be a problem too. If you want different action from the buttons - give it different names. isn’t it obvious?
Do you give the same name to all your text inputs?