PHP,单选按钮返回开启,而不是值

4

抱歉,如果这是一个非常简单的问题,因为我正在学习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>';

我的网页的HTML,由我的PHP生成,非常不完整,如下所示:

<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。

注:该段内容与IT技术无关。

3个回答

7
您忘记在每个单选控件的name属性中添加闭合引号:
<input type="radio" name="f0" value="radio">
<!--                        ^ this here is important -->

因此,您的浏览器认为该电台的名称属性是"f0 value=",并且缺少值属性。因此,它会添加默认属性on

非常感谢,我现在感觉真的很愚蠢,它只是在那里所有其他的“和'中混合在一起。 - Hovestar

1

这将是Web设计中显而易见的答案。
你准备好了吗?...
确保为每个单选按钮分配一个值。
我花费了2.5个小时(令人沮丧和困惑)的研究才意识到我疏忽了这一点。每个按钮都有一个唯一的ID,我希望使用以下方式返回它们:

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

Which (of course) had no effect on this 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 finally had the correct value (not just 'on') with the following 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>

Turns out an id is not a value. So unreasonable! :-)


1
尝试一下。
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>

如果您还有其他语句,请确保使用"正确关闭它。


1
非常感谢,我现在感觉真的很愚蠢,它只是在那里所有其他的“和'中混合在一起。 - Hovestar
我们都会犯错误。 :p - Sizzling Code

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接