cw84
cw84

Reputation: 2190

Only allowing selection of one radio button in a form with PHP

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! :)

Upvotes: 4

Views: 33645

Answers (2)

lopamudra
lopamudra

Reputation: 49

Every input should have same "name"

Upvotes: 2

instanceof me
instanceof me

Reputation: 39208

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.

Upvotes: 23

Related Questions