views:

903

answers:

4

When using multiple button elements in a form, I realised that IE7 sends the innerHTML instead of the value of the button. All good I thought, I'll simply change my PHP code to this

<?php

if (isset($_POST['button-name'])) {
   add_product_to_cart(2);
}

?>

Now my old friend IE6 is going a little step further at being a nuisance. It sends all of the button elements reguardless of which one I click. For example, I have 3 button elements named 'mint', 'near-mint' & 'standard'. A quick print_r($_POST) tells me that all 3 names have been submitted.

I guess to remedy this will be some JavaScript, not the most elegant situation, but I can imagine that the average user still using IE6 is not bright enough to turn off their JavaScript (and the client never specified handheld use).

Has anyone built a javascript to remedy this, or if possible, a jQuery plugin? Thank you

+2  A: 

I found a solution at http://www.codecomments.com/JavaScript/message756646.html

All credit to the author on that page.

Per request, here is the code

function buttonfix(){
var buttons = document.getElementsByTagName('button');
for (var i=0; i<buttons.length; i++) {
buttons[i].onclick = function () {
for(j=0; j<this.form.elements.length; j++)
if( this.form.elements[j].tagName == 'BUTTON' )
this.form.elements[j].disabled = true;
this.disabled=false;
}
}
}
window.attachEvent("onload", buttonfix);
alex
You should post the code you used so people who Google this page will be able to find your solution. (The page you linked to may not last forever.)
titaniumdecoy
A: 

You should check for the button's value.

http://www.w3schools.com/tags/tag_button.asp

Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and the W3C specification) it is "submit".

Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the <button> and </button> tags, while other browsers will submit the content of the value attribute. Use the input element to create buttons in an HTML form.

rick schott
IIRC I tried this, but did not work.
alex
How are you submitting the form? With a submit button or javascript?
rick schott
clicking the button sends the form elements to the action attribute in my form.
alex
This doesn't solve the problem of IE considering all buttons to be successful controls even if not clicked.
David Dorward
A: 

An solution is to use <input type="button"> and <input type="submit"> in your page instead of <button type="button"> and <button>.

Update: if you change your button elements to input elements you should be able to find them using jQuery with the following selector:

var buttons = $('input[type=submit], input[type=button]');
wrumsby
Except I had 4 buttons.
alex
How does having 4 buttons prove to be a problem?
Simon Lieschke
Because I need to know exactly which one was clicked.
alex
So replace each button with an input?The point I was trying to make is that the submission bug only affects button elements and does not affect input[type=submit] and input[type=button] elements.You should be able to find these in jQuery using the selector: var buttons = $('input[type=submit], input[type=button]');
wrumsby
I used buttons because I needed to style them with images and text. Also, finding them with jQuery doesn't solve my problem. My problem is IE6 submits all values from all buttons regardless of which one is pressed. See the link in the accepted answer.
alex
+1  A: 

This is a known bug in Internet Explorer.

http://www.dev-archive.net/articles/forms/multiple-submit-buttons.html describes a workaround that does not depend on JavaScript.

It boils down to "Use <input> and don't design your buttons to need anything other than simple text for their labels".

David Dorward