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