问题
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.
form
$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
<?php
$q_nr = $_GET['q_nr'] ;
echo $q_nr;
?>
回答1:
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>";
And on answer.php:
//Key is $q_nr and $answer is selected $option
foreach($_POST['question'] as $key => $answer) {
echo $key;
}
回答2:
First your form submission method is POST and your are retrieving in GET,
Secondly its not going to work dude, you are creating many forms in loop, It is a logic realted problem, put your FORM out of the loop and make the elelent an array like q_nr[] -------
回答3:
If FORM METHOD equals POST you get the params in $_POST superglobal variable:
$q_nr = $_POST['q_nr'] ;
来源:https://stackoverflow.com/questions/7510546/html-form-name-php-variable