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

孤人 提交于 2019-12-22 08:52:42

问题


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


回答1:


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.




回答2:


Every input should have same "name"



来源:https://stackoverflow.com/questions/991025/only-allowing-selection-of-one-radio-button-in-a-form-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!