alex
alex

Reputation: 490263

Is there a way to identify forms if using the button element to submit?

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

Upvotes: 2

Views: 1049

Answers (3)

goat
goat

Reputation: 31813

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.

Upvotes: 2

Ankit Sachan
Ankit Sachan

Reputation: 7840

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

Upvotes: 0

o.k.w
o.k.w

Reputation: 25810

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>

Upvotes: 4

Related Questions