【问题标题】:PHP, Radio Buttons return on, not valuePHP,单选按钮返回,而不是价值
【发布时间】:2013-04-22 07:41:19
【问题描述】:

对不起,如果这是一个非常简单的问题,我只是在学习 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>

我专门切换到获取而不是发布,这样我就可以看到我的结果,这是我的网址 /index.php?f0+value%3D=on&amp;f1+value%3D=on&amp;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 太长了。

【问题讨论】:

    标签: php radio-button


    【解决方案1】:

    您忘记了在您的单选控件的每个名称属性中都有一个“结束符”:

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

    因此,您的浏览器认为此收音机的名称属性是"f0 value=",并且缺少值属性。因此它添加了默认属性on

    【讨论】:

    • 非常感谢,我现在觉得自己很愚蠢,它只是融入了所有其他的“和”。
    【解决方案2】:

    试试这个。

    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>
    

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

    【讨论】:

    • 非常感谢,我现在觉得自己很愚蠢,它只是融入了所有其他的 " 和 ' 周围。
    • 我们都会犯错。 :p
    【解决方案3】:

    这将是明显的网页设计学院的答案。
    你准备好了吗? ...
    确保您确实为每个单选按钮分配了一个值。
    我花了 2.5 小时(令人沮丧和困惑)的研究才意识到我忽略了这样做。每个按钮都分配了一个唯一的 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 不是一个值。太不合理了! :-)

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 2020-05-15
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2016-04-15
      • 1970-01-01
      • 2014-04-05
      相关资源
      最近更新 更多