开发者

same name radio fields

开发者 https://www.devze.com 2023-02-24 04:08 出处:网络
Excuse me, but I have no idea what this is called, so I\'ll try and explain it. With HTML forms you can have input fields of the same name, with auto assigning keys for each one, for example:

Excuse me, but I have no idea what this is called, so I'll try and explain it.

With HTML forms you can have input fields of the same name, with auto assigning keys for each one, for example:

<form action="somepage" method="post">
<input type="text" name="phone[]" />
<input type="text" name="phone[]" />
<input type="submit" />
</form>

when this form is submitted, the server receives the data in via POST.

as an associative array it looks like this:

Array(
[phone] => Array ( [0] => 123456789 [1] => 987654321 )
)

where keys 0,1 are given automatically.

how would you do the same with radio fields?

<input type="radio" name="option[]"/>
<input type="radio" name="option[]"/>

is t开发者_开发技巧reating both fields as one (as it should) and not giving it unique keys...


As far as HTML is concerned, fields with the same name are just fields with the same name. Radio buttons are a special case in that sharing a name makes them part of a group and only one may be selected.

In most form handling libraries, you can get data from multiple elements with the same name presented as an array.

In PHP, you can only get the data presented as an array if the name ends with [] (or [something] for arrays with predefined indexes).

Only checked radio buttons can be successful and only one radio button in a group can be checked (that is the point of radio buttons).

If you want the user to pick multiple options, use checkboxes. If you give those checkboxes the same name and end it with [] then PHP will get all the values of the checked checkboxes in a single array.


It is possible to specify an array key in your field name.

<input type="radio" name="option[radiooption1]" />

I believe it works for numeric keys as well, but I haven't tested. See http://php.net/manual/en/faq.html.php for more.


the radio button can not be posted as an array like that you will only ever make post the one value so i fail to see how giving it an index helps anything. even if you manually ad the index like this

 <input type="radio" name="option[1]" />
 <input type="radio" name="option[2]" />

your still only going to get one item in the option array

in the above example the radio buttons would not work correctly the would no longer act the way a radio button should it would they would each be a separate radio see this DEMO

My point is this the radio button is a choice of element where you give the user a number of choices allowing them to only choose ONE so posting any other data related to the radio button of there choice is pointless because all you care about is what there choice is.


I was doing something similar in VueJS where I duplicate a fieldset of set questions x amount of times. I needed the values to be handled by php as arrays so had name attributes of name_x[] name_y[] etc.

This caused issues with duplicated radio buttons only allowing an answer for a fieldset question accross all instances

I found that if I added the index of the fieldset to the input name rather than leaving the array empty it gave the same result on the backend and now worked on the front end.

Loose example using index (0,1,2...) instead of leaving empty []:

<fieldset v-for="(question,index) in questions">
  <label> question </label>
  <input name="question[index]" type="radio" value="yes">
  <input name="question[index]" type="radio" value="no">
</fieldset>
0

精彩评论

暂无评论...
验证码 换一张
取 消