views:

42

answers:

3

I've started using the button element to submit my forms

<button type="submit">Send</button>

I know if I used an input element like so, I could easily determine in PHP which form was submitted (by reading the value of $_POST['form'] in the example below).

<input type="submit" value="Send" name="form" value="contact" />

Is there a way to do this using the button element? There will be one button element per form. I know that IE6 has problems with multiple button elements in one form, and IE7 sends the innerHTML instead of the value.

Would this be reliable?

HTML

<button type="submit" name="form-contact" value="true">Send</button>

PHP

if (isset($_POST['form-contact'])) {
    // Contact Form submitted
}

Many thanks

+3  A: 

This is not a direct answer to you question, but you can always use hidden inputs to distinguish the forms:

<form>
<input type="hidden" name="form-name" value="form-1" />
</form>

Another form:

<form>
<input type="hidden" name="form-name" value="form-2" />
</form>
o.k.w
Ah yeah I knew about that, just wondering if I could get away with adding less to my forms.
alex
A: 

you can use AJAX to send form_name corresponding to clicked button, just embed the name of form along the url

Ankit Sachan
Ajax should only be used where it improves usability for the user.
Felix Kling
+2  A: 

If the script which processes the form needs to distinguish between different forms, you should use a hidden input, because it's reliable cross browser. Testing for the name/val of an submit button isn't very reliable because some browsers simply won't send the name/value pair of the submit button if the user submits the form via the enter key while a different form element has focus. Form processors that fail in this common scenario are really lame. button, input type=button, and input type=image all have cross browser inconsistencies.

You rarely even need the hidden input actually. You can just use different input names for different forms. eg name=contact_email, name=order_email etc.. and then just test which one exists to determine form identity. But, this gets more complicated and I personally don't feel it's worth the extra effort when you can just put the hidden input there.

chris