Why isset($_POST['submit']) is not compatible with Internet Explorer

Using isset($_POST['submit']) to check if a form has been posted or submitted is bad practice because it is not compatible with Internet Explorer.

This method relies on the submit button of the form being present in the $_POST array. The problem is Internet Explorer does not send the submit button as a post variable if the user pressed the enter key to submit the form.

Here is a typical example:

PHP Code <?php

    
if(isset($_POST['submit']))
    {
        
// a form was submitted (POSTED)
    
}

?>

I see this all the time even with intermediate and above programmers. Perhaps the problem is with many poor quality or out of date online PHP tutorials that are available.

If you are relying on this method and the user used the enter key to submit the form then most likely what would happen is the page would appear to simply refresh. Obviously this could be very frustrating for the user, particularly if it was a long form. This is an accessibility failure, users should always be able to use the enter key to submit forms. An example is a special browser that caters for disabled users who are not able to use a mouse, or a user with any browser without a mouse.

The solution

The best practice for checking if a form is submitted is to use the server variable REQUEST_METHOD. This variable will be set to POST if a form was submitted (providing the form had a method of POST. Below is the best practice for detecting form submission with PHP.

PHP Code <?php

    
if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        
// a form was submitted (POSTED)
    
}

?>

The above works on all browsers because it is not reliant on the submit button being present in the post variables.

Comments

There are 0 responses. Why not add a comment? No registration required.

Leave a Comment

Enter Code

Refresh code

Menu

Firefox Addons