html form name= php variable

亡梦爱人 提交于 2020-01-07 00:36:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!