Another Reason Why IE Ruins My Motivation to Code
Have you ever known something to be true but never really had it sink in until you experience it first hand? For quite some time I’ve known that detecting form submissions on the server side by testing for the presence of a submit button is a bad idea. I just never understood why until today.
Assume a form that requires the use of two submit buttons:
This interface pattern is commonplace in content management systems where “Edit” and “Delete” actions are assigned to a large group of items in a record set — especially where batch operations are required (IE: edit/delete multiple records at once). At first glance this doesn’t seem like that big of a deal. Your server-side parser would look similar to the following (of course, the real parsing code would replace the print_r() call):
To test this open up a browser fill out the form and hit “Edit”. You should see:
Array ( [test] => hi [submit] => Edit )
Similarly if you hit the “Delete” button:
Array ( [test] => hi [submit] => Delete )
The headache comes when you don’t use either one of the buttons. Using the Enter key on the keyboard is a well-known technique for form submission used by armatures and well-seasoned computer users alike. It works both on the OS-level as well as within a web browser. In other words, this is a common behavior and should be planned for.
So what happens? Try it out. If you’re on a normal browser, you should see that it accepts the user input, and uses the first submit button it sees to send the data. If you’re using IE, you see something very different: nothing at all. This is because of specific implementation of the conditional used to detect if a form was submitted. It looks specifically for the $_POST['submit'] value that should be passed. However, because you’re using forms improperly, IE punishes you by not allowing a submit element to be passed.
A solution I’ve been using for years is to test to see if $_POST is not empty instead of looking for a specific element.
As promised, the form now submits under both browsers. The remaining problem is that IE still doesn’t pass the submit field with the rest of the posted data:
Array ( [test] => hi )
So how do we know whether or not to delete? You can assume that when no submit field is present, a user wants to edit a record instead of deleting it. Really, it’s only one little conditional:
Sure, it works. But for how long? What happens if the order of the buttons changes to Delete coming first and Edit coming second? Now you have to update your code in two places in order for your application to behave how they expect it to.
Terrible.
4 Responses to Another Reason Why IE Ruins My Motivation to Code
C.Barr:
crap, i can’t post code?
Lets try this instead:
<a class="button negative cancel" href="index.php">Cancel</a>
<a class="button negative delete" href="delete.php?id=12">Delete</a>
<button type="submit" class="positive save">Save</button>
mikeg:
Indeed that looks promising, and yes, I’ve reworked the application to fit that pattern but IE is still behaving oddly.
In one instance, I had a single line select menu with two button elements acting as submits it would always take the last button as the submit button (unless floated). I’m still in the process of discovering the quirks. Unfortunately it doesn’t seem consistent from version to version — which makes the process even more fun.
I’ll post the results as soon as I get more concrete examples. The process is slow as I don’t have a PC to test on ![]()
bslag:
Little late, but you have forgotten a ) in the final bit of code…
if(!isset($_POST[’submit’])) {
I spent half an hour thinking to myself.. why wouldn’t Mike G’s code work?! Surely he doesn’t make any mistakes. I write it myself in a slightly different manner, and viola.
SHAME ON YOU MIKE G! :p
Comments are closed. Sorry.
C.Barr:
On July 13th, 2007 at 11:31 pm #
I just use the button html elements for buttons, and then style links for non-submit actions. for example:
Cancel
Delete
Save
I based my button styles off of this: http://particletree.com/features/rediscovering-the-button-element/
Now this may not suit your needs, and would probably not help at this point, or even at all maybe, but this is what I do.