Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 471
cw84 Avatar asked Nov 29 '22 05:11

cw84


1 Answers

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.

like image 162
instanceof me Avatar answered Dec 10 '22 23:12

instanceof me