在php中的RadioButtons

时间:2016-06-15 19:20:44

标签: php html radio-button

我有一张表格:

    <form  action= "catrand.php" method="POST" name="form">
    <h4>How mach text need to print?</h4>
    <input type="text" onkeyup="validate(this)" name="inp"> <script> function validate(inp) { inp.value = inp.value.replace(/[^\d,.]*/g, '') .replace(/([,.])[,.]+/g, '$1') .replace(/^[^\d]*(\d+([.,]\d{0,5})?).*$/g, '$1'); } </script>
    <h4>Select how print text</h4>
<input type="radio" name="b001" value="First"><font>text</font>
<input type="radio" name="b001" value="Second"><font>check</font>
<input type="radio" name="b001" value="Last"><font>radio</font>
    <h4>Input text</h4>
    <input type="text" name="inp1">
    <input type="text" name="inp2">
    <input type="text" name="inp3">
    <p><input type="submit"></p>    
    </form>

和php代码:

for($i=1;$i<=$number;$i++)
    {
        switch($_POST['b001']) {
            case "Last":
            {
                echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"First\" \"name=\"h001\">" .$numberone; 
                echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"Second\" \"name=\"h001\">".$numbertwo; 
                echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"Third\" \"name=\"h001\">".$numberthree."</br>";
                break;
            }


}           
    }

但尽管radiobuttons的名称相同,我可以标记两个或更多的radiobuttons。为什么?我的代码出了什么问题?

3 个答案:

答案 0 :(得分:1)

您不需要此处的转义引号:\"name=只需name=

答案 1 :(得分:0)

请查看此页面Only allowing selection of one radio button in a form with PHP

尝试添加&#39; for&#39;属性如该页面上的答案所述。

答案 2 :(得分:0)

问题在于你的PHP代码

更改您的代码如下

for($i=1;$i<=$number;$i++)
{
    switch($_POST['b001']) {
        case "Last":
        {
            echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"First\" name=\"h001\">" .$numberone; 
            echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"Second\" name=\"h001\">".$numbertwo; 
            echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"Third\" name=\"h001\">".$numberthree."</br>";
            break;
        }}}

不要在名字之前使用\。