PHP,单选按钮返回on,而不是value


PHP, Radio Buttons return on, not value

对不起,如果这是一个非常简单的问题,我只是学习php,但我一直在写一个脚本,可以创建测验,现在html讨厌我。当我单击单选按钮并提交表单时,结果总是按钮处于打开状态,而不是我分配给它的值。以下是我的代码摘录:

echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
    echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
    echo '<input type="radio" name="f' . $i . ' value="radio">Radio Buttons <br>';
    echo '<input type="radio" name="f' . $i . ' value="text">Text area <br>';
    echo '<input type="radio" name="f' . $i . ' value="Checkboxes">Checkboxes <br>';

我的页面,这是非常不完整的,由我的php生成的html是这样的:

<html>
<body>
<form action="index.php" method="post">
Number of questions: 
<input type="number" name="fsize">
<input type="submit">
</form>
<form action="index.php" method="get"> 
1: 
<br> 
<input type="radio" name="f0 value="radio">
Radio Buttons 
<br> 
<input type="radio" name="f0 value="text"> 
Text area  
<br> 
<input type="radio" name="f0 value="Checkboxes"> 
Checkboxes  
<br> 
1:  
<br>  
<input type="radio" name="f1 value="radio"> 
Radio Buttons  
<br> 
<input type="radio" name="f1 value="text"> 
Text area  
<br> 
<input type="radio" name="f1 value="Checkboxes"> 
Checkboxes  
<br> 
1:  
<br>  
<input type="radio" name="f2 value="radio"> 
Radio Buttons  
<br> 
<input type="radio" name="f2 value="text"> 
Text area  
<br> 
<input type="radio" name="f2 value="Checkboxes"> 
Checkboxes  
<br> 
<input type="submit"> 
'n  
</form>
</body>
</html>

我特别切换到get而不是post,所以我可以看到我的结果,这是我的url /index.php?f0+value%3D=on&f1+value%3D=on&f2+value%3D=on的重要组成部分

有谁知道我怎么才能让它正确提交吗?如果你想看完整的PHP脚本在这里:

<html>
<body>
<?php
session_start();
echo '<form action="index.php" method="post"> Number of questions: <input type="number"
name="fsize">';
echo '<input type="submit"></form>';
if (isset($_POST["fsize"]))
{
$_SESSION["fsize"] = $_POST["fsize"];
}
if (isset($_SESSION["fsize"]))
{
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
    echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
    echo '<input type="radio" name="f' . $i . ' value="radio">Radio Buttons <br>';
    echo '<input type="radio" name="f' . $i . ' value="text">Text area <br>';
    echo '<input type="radio" name="f' . $i . ' value="Checkboxes">Checkboxes <br>';
    if (isset($_GET["f" . $i]))
    {
        $_SESSION["f".$i] = $_GET["f".$i];
        echo "I exist!";
    }
    if (isset($_SESSION["f".$i]))
    {
        echo '<textarea rows="4" cols="50"> What is the question? </textarea>';
        switch($_SESSION["f".$i])
        {
            case("radio"):
                echo "you chose a radio button!";
                break;
            case("text"):
                echo "You chose a text area!";
                break;
            case("Checkboxes"):
                echo "You chose checkboxes!";
                break;
            default:
                break;
        }
    }
    else if (isset($_GET["f".$i]))
    {
        echo '<textarea rows="4" cols="50"> What is the question? </textarea>';
        switch($_GET["f".$i])
        {
            case("radio"):
                echo "you chose a radio button!";
                break;
            case("text"):
                echo "You chose a text area!";
                break;
            case("Checkboxes"):
                echo "You chose checkboxes!";
                break;
            default:
                break;
        }
    }
}
echo '<input type="submit"> </form>';
}
?>
</body>
</html>

我知道它还有其他一些问题,但这是最让人恼火的。很抱歉,Hovestar。

您忘记在您的单选控件的每个name属性中使用结束符":

<input type="radio" name="f0" value="radio">
<!--                        ^ this here is important -->

因此,您的浏览器认为该广播的name属性是"f0 value=",并且缺少value属性。因此它添加了默认属性on

试试这个

echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
    echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
    echo '<input type="radio" name="f' . $i . '" value="radio">Radio Buttons <br>';
    echo '<input type="radio" name="f' . $i . '" value="text">Text area <br>';
    echo '<input type="radio" name="f' . $i . '" value="Checkboxes">Checkboxes <br>

如果您有更多的语句,请确保使用"正确关闭它。

这将是来自Captain Obvious网页设计学院的答案。
你准备好了吗?...
确保您实际为每个单选按钮分配了一个值。
我花了2.5个小时(令人沮丧和困惑的)研究才意识到我忽略了这一点。每个按钮都分配了一个唯一的id,我希望使用

返回这个id:

showAnswerWhen = $('input:radio[name="score"]:checked').val();

这当然对这个HTML没有影响:

        <div class="pull-right score-bar">
            Show answers:
            <input type="radio" name="score" id="immediately"/>
            <label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="immediately">Immediately</label>
            <input type="radio" name="score" id="when" checked/>
            <label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="when">When I answer</label>
            <input type="radio" name="score" id="end"/>
            <label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="end">At the end</label>
        </div><br/>

showAnswerWhen终于有了正确的值(不仅仅是'on'),下面的HTML:

    <div class="pull-right score-bar">
            Show answers:
            <input type="radio" name="score" id="immediately" value="immediately"/>
            <label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="immediately">Immediately</label>
            <input type="radio" name="score" id="when" value="when" checked/>
            <label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="when">When I answer</label>
            <input type="radio" name="score" id="end" value="end"/>
            <label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="end">At the end</label>
        </div>

显示id不是一个值。所以不合理!: -)