使用单选按钮作为复选框

时间:2013-02-09 07:13:26

标签: php javascript html css forms

我正在学习如何从this tutorial series开发WordPress插件,现在我在this tutorial,但我发现了一个奇怪的事情:使用了两个RadioButtones来获取值“True”或“False”他们可以使用一个CheckBox

<label for="devloungeHeader_yes">
    <input type="radio" id="devloungeHeader_yes" name="devloungeHeader" value="true" <?php if ($devOptions['show_header'] == "true") { _e('checked="checked"', "DevloungePluginSeries"); }?> />
    Yes
</label>&nbsp;&nbsp;&nbsp;&nbsp;
<label for="devloungeHeader_no">
    <input type="radio" id="devloungeHeader_no" name="devloungeHeader" value="false" <?php if ($devOptions['show_header'] == "false") { _e('checked="checked"', "DevloungePluginSeries"); }?>/>
    No
</label>

我在joomla管理页面上看到类似的东西,其中使用了2个单选按钮来获得相同的结果。

那么,使用两个单选按钮而不是一个复选框是否有(html,css,javascript,php,只是任何一种)优势?

1 个答案:

答案 0 :(得分:0)

实际上我发现为什么使用两个具有相同名称和不同值的单选按钮“True”和“False”比复选框更好。如果未选中复选框,则不会将其提交给服务器。但是当你使用带有“True”和“False”值的单选按钮时,这些单选按钮将在服务器上提交,因此php无论如何都会从html表单获得结果。因此,使用ckeckbox,您只有两个选项:

1) when chebox is checked and is submited (value == "checked")
2) when checkbox is Not checked and it is not submited

使用php你有两个选择:

<?php
if (isset($_POST['testCheckBox'])) {
    // it is submited so it means it is Checked!
} else {
    // it is not submited because it is not checked or there is something wrong!
}
?>

但是使用单选按钮有三个选项:

1) when radio "True" is checked and is submited
2) when radio "False" is checked and is submited
3) when radio is not SUBMITED, so something is not working properly

php示例:

<?php
if (isset($_POST['testRadioButton'])) { // testRadioButton is submited!
    if($_POST['testRadioButton'] == "true"){
        // True is checked, do something
    } elseif(($_POST['testRadioButton'] == "false"){
        // False is checked, do something
    }
} else {
    // it not Submited! so there is definitely something wrong with code
}
?>