views:

444

answers:

1

A very basic question...

How can I only allow the selection of one option in a list of radio buttons?

<form action="process_repair.php" method="POST">
 <label for "repair_complete">Repair complete</label>
  <input type="radio" name="Yes" value="true">
  <input type="radio" name="No" value="false">
</form>

When this code runs, it's possible to select both radio buttons, but I'd like them to interact so you can only select one or the other.

Any help much appreciated! :)

+5  A: 

Give them the same name.

<form action="process_repair.php" method="POST">
 Repair complete
 <input type="radio" name="complete" value="true" id="complete_yes" />
 <label for="complete_yes">Yes</label>
 <input type="radio" name="complete" value="false" id="complete_no" />
 <label for="complete_no">No</label>
</form>

Labels must have a for attribute, directing to the corresponding input's id.

streetpc
You can also just do <label><input ...> Label text</label>.
Paul Fisher
Do note that the implicit linking (comment above) is deprecated as of XHTML 1.1(?).
antennen
@antennen I can't see any mention of that in the XHTML 1.1 spec, but implicit labeling doesn't enjoy as wide support as with the for attribute.
David Dorward
It is also worth pointing out (given the question) that the "select one radio in a group" is a function of HTML, not PHP. You just need to assign them all to the same group (by giving them the same name).
David Dorward