0

What HTML attribute will prevent form data from posting if two inputs have the same name attribute?

<form>
<select name="amount">
  <option value="100">$100</option>
  <option value="50">$50</option>
  <option value="10">$10</option>
  <option value="1">$1</option>
</select>
Other: <input type="text" name="amount">
</form>

Edit: The reason I need two with the same name value is I'm using the jQuery to show() and hide() functions for a select and input.

Conor
  • 168
  • 9
  • The `disabled` attribute comes to mind. – Musa May 12 '14 at 13:05
  • why not just use 2 different names and then set your variable to the text one if it isn't empty – Pete May 12 '14 at 13:09
  • I've tried both `disable` and `read-only` with no success. The reason I need two with the same name value is I'm using `jQuery` to show() and hide() functions for a select and input. – Conor May 12 '14 at 15:23

1 Answers1

0

There is no such HTML attribute. It's perfectly legal for two elements to have the same name, and both values will be included in the data that is posted.

The values will be sent as two separate items, so the posted data from the form in the question could for example look like this:

amount=10&amount=42
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • How can I pull it into PHP separately? `$_POST['amount[1]']`? – Conor May 13 '14 at 17:34
  • 1
    @Conor: If you can use the name `amount[]` instead of `amount` in the HTML code, then PHP will return them as an array from `$_POST['amount']`. If not, then you can read them from `php://input`. Here are some examples: http://stackoverflow.com/a/2203456/69083 – Guffa May 13 '14 at 17:46