![]() |
The IE if (isset($_POST['submit'])) bug explained.
For a while now I've had a clear warning in my signature about not using this technique. I've since then received a lot of questions about it via PM and this week alone I've had quite a few. Today I am going to explain this issue to you in further detail so that you can understand and try this out for yourself and fix your code.
Lets start with a basic script: PHP Code:
Why? Because Internet Explorer only sends the button if you click it with the mouse. If you have the text cursor in a text box and click the enter / return key on your keyboard IE does not send the value of the submit button. This is because you can use multiple submit buttons in one form (EG Edit and Delete on a blog / forum post) and so MS in their wisdom seem to have decided that only sending a clicked button is the wise thing. There is some wisdom in this but only for forms with multiple submit buttons. For forms with just one submit its a bit pointless. I've heard that on IE9 this is no longer a problem but I've tested this on IE5.5, IE6, IE7 and IE8 and it is the same on all of them. IF you do not experience the same symptoms and do not believe it, then please watch this video: Once you've watched that, I hope you will understand how this could affect your logic flow in your code. This issue could loose you orders, new members, contact form submissions etc. By using the simple techniques I've demonstrated below, you will no longer have this issue and it will work regardless of what browser your visitor uses. Now, lets modify that script: PHP Code:
There is a further way to handle this - use a hidden form field: PHP Code:
If you feel that this topic has helped you, there is a green "Thank user for this post" button below :thumbsup: |
I suppose its forturnate that i have always done isset on a submitted field and not the submit button value. Not because i knew this was happening but just because i wanted control over my switch (me being anal lol) and It just turned out to be lucky that i didnt do it on the button afterall. But i do have open source scripts that i use from others that do use this,so thanks for this, those will need to be corrected.
Thanks again tango, great post. :thumbsup: |
I'll add this thread to the "list".
One thing to note is that IE's behaviour contravenes the specification for HTML form processing indicated here: http://www.w3.org/TR/html4/interact/...html#h-17.13.2 (which will come as no surprise to any client developer :P). From the PHP world, you should actually check that every required field has been provided: PHP Code:
|
Does this bug also affect images as since most form nowadays have an image as a submit button:
|
tango while I agree with you, this only occurs if you have only one text input in your form and then a submit button. If you more than one text input hence your example it will send the post from the submit button.
Most forms will have way more than one input of type text but for those that don't yes you need to check for the post of a different field. If you have more than one text/hidden field you can now for isset($_POST['submit']) or whatever your submit button may be named and it will work regardless of clicking on it with your mouse. It does not work like I have mentioned if you use the hidden input as your second input. An example of what I mean The following will pass in the post for the submit button. PHP Code:
|
Quote:
|
Quote:
Good bit of info there but I think it's best to still advise people not to use the submit button as it's too much hassle to expect people to remember more than one field or they'll get trouble. They'll forget about it, create a form with one field and then start head scratching. Best way to keep it simple is just to recommend people use the submit button. |
Quote:
Thre is a second issue regarding using name="submit" in submit buttons that your above explanation doesn't mention. In some versions of Internet Explorer the names are mapped directly to JavaScript variables. Specifying a submit button with that name will therefore map form.submit to the submit button. This then makes it impossible to actually submit the form from JavaScript. The safest thing to do if you only have the one submit button in your form is to not give it a name at all. |
Tested everything (from the posts above) in IE6 and IE7 and verified what you wrote apart from when I replaced:
<input type="submit" name="submit" value="Then the next time, click this">with: <INPUT TYPE="image" SRC="images/signup.png" HEIGHT="25" WIDTH="63" BORDER="0" ALT="Submit Form">Tangoforce Quote:
Code:
Your data was processed!This also avoids the second issue with javascript (described by felgall) because the image input type doesn't have a name field. Would love to hear thoughts on this as this is the solution I have to use.. |
just out of curiousity, does anyone know if this bug is still in IE, as Ive seen on TV the Internet Explorer 9 is coming out/ Already out?
|
Quote:
While I don't doubt that there may be truth in what you're saying, I never said that it wouldn't work (thats in reference to your "Not true" remarks). I simply said the image had no name="" or value="" attributes and therefore they would not be sent. It's possible that IE (in relation to felgalls comments) will automatically send a $_POST['submit'] field for an image based submit - this could be a empty value or an array of x and y as felgall has mentioned. If it does, then that is a completely different issue and not related to the submit button issue. To comment any further on your data processed message you'd need to post your complete code for both the html and the php. Quote:
I never expected this topic to become so popular so many months after posting it! |
Quote:
|
Quote:
<input type="submit" name="submit" value="Then the next time, click this">with: <INPUT TYPE="image" SRC="images/signup.png" HEIGHT="25" WIDTH="63" BORDER="0" ALT="Submit Form">Quote:
Quote:
Quote:
|
Quote:
To be honest I see it as a null issue anyway when the most reliable way is to use a hidden field / value. Relying on the $_POST['submit'] value is asking for trouble whether you like to admit it or not. By using the hidden field you guarantee that your script will work. Quote:
|
Agreed. I don't fully understand or trust IE's behaviour so I'm using your hidden field solution (even though my form has more than 1 field).
|
| All times are GMT +1. The time now is 07:08 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.