Home > General Site Maint > Return key doesn’t play nice in web form using Internet Explorer

Return key doesn’t play nice in web form using Internet Explorer

September 26th, 2008 ^Lestat Leave a comment Go to comments

I ran across an interesting situation where some users of mine were complaining that a form I created “didn’t work”. Kind of strange to get this complaint after almost a full year of operation. I remember testing the xhtml and css design in both firefox and IE browsers and it all checked out. What I didn’t think of checking was the behavior of browsers when one presses the return key. With the help of Fiddler (thanks to Casey Williams for the Fiddler suggestion) this is what I discovered:

IE Firefox
Submit Button Clicked All inputs $_POSTed All inputs $_POSTed
Return Key Pressed All inputs $_POSTed
except form buttons
All inputs $_POSTed

(For a larger, more detailed image click here)

As you can see, when my IE users were pressing the return key, the button itself was not being $_POSTed. So what right? Well what if you were checking if the submit button was pressed before doing any processing? Like so…

< ?php
if(isset($_POST['button_name'])){
    // validate the user entries...
}
?>

This would fail if the user hit the return key in IE7. I haven’t tested earlier versions.

View the example. Test it in both browsers.

Right now the only thing that was suggested to me was to add a hidden field to mimic the submit button in your form like so:

<form ...>
    <input type="hidden" name="button_name" value="whatever"... />
</form>

That works just fine.

From perusing irc and several forums, the only response as to WHY this happens are comments like “It’s IE, what do you expect?”. I’m already annoyed that IE doesn’t think it has to adapt to web standards, but this ont really caught me off guard.

I’m wondering if any readers might have some input on my inputs ;P

Categories: General Site Maint Tags:
  1. Luke
    September 29th, 2008 at 04:33 | #1

    I had a really large script that i made stop functioning at one point because of this error and i now use exactly the same solution you use, a hidden variable set to 1 that i check for.

    I get the feeling this might have been implemented eventually on IE but i’m not 100% sure, it caused me alot of headaches.

  2. Thaddeus
    December 21st, 2008 at 12:41 | #2

    You can say that again.

  3. March 18th, 2009 at 02:35 | #3

    thanks for this code i think most of coder know about thin code and it so simple

  4. July 23rd, 2009 at 05:30 | #4

    I am using it right now. For the most part it is ok, but there are some sites, like my bank, that don’t recognize it as a browser that is compatible. I love the tabs a lot. For some reason, I do have problems adding attachments in yahoo mail with IE7.

  1. No trackbacks yet.