html表单名称= php变量

问题描述:

我有一个用户必须填写的表单(基本上是一个测试)。我从MySQL表中得到的问题,但是我无法将问题编号传递给answer.php文件。

I have a form (basically a test) that the users has to fill in. The question nr I get from the MySQL table but I can not get the question number carried over to the answer.php file.

表格

$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY RAND() LIMIT 5";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_array($result1))
{
    $test_name=$row1['test_name'];
    $q_nr=$row1['q_nr'];
    $q_type=$row1['q_type'];
    $question=$row1['question'];
    $option1=$row1['option1'];
    $option2=$row1['option2'];
    echo "<form method='post' action='answer.php'>";
    echo "<P><strong>$q_nr $question</strong><BR>";
    echo "<input type='radio' name='$q_nr' value='option1'>$option1<BR>";
    echo "<input type='radio' name='$q_nr' value='option2'>$option2<BR>";
    echo "<BR>";
    echo "<BR>";
    echo "</p>";
}
echo "<input type='submit' value='Send Form'>";
echo "</form>";
?>

answer.php

answer.php

<?php
$q_nr = $_GET['q_nr'] ;
echo $q_nr;
?>


我假设您想获得所有问题并显示它们在一个页面上,然后提交answer.php的所有答案?在这种情况下,您可以:

I assume you want to get all questions and display them on the one page and then submit all answers to answer.php? In that case you could:

$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY RAND() LIMIT 5";
$result1=mysql_query($sql1);

echo "<form method='post' action='answer.php'>";

while($row1 = mysql_fetch_array($result1))
{
    $test_name=$row1['test_name'];
    $q_nr=$row1['q_nr'];
    $q_type=$row1['q_type'];
    $question=$row1['question'];
    $option1=$row1['option1'];
    $option2=$row1['option2'];

    echo "<P><strong>$q_nr $question</strong><BR>";
    echo "<input type='radio' name='question[$q_nr]' value='$option1'>$option1<BR>";
    echo "<input type='radio' name='question[$q_nr]' value='$option2'>$option2<BR>";
    echo "<BR>";
    echo "<BR>";
    echo "</p>";
}
echo "<input type='submit' value='Send Form'>";
echo "</form>";

并且在answer.php上:

And on answer.php:

//Key is $q_nr and $answer is selected $option
foreach($_POST['question'] as $key => $answer) {
    echo $key;
}