html表单名称= php变量 [英] html form name= php variable

查看:85
本文介绍了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;
}

这篇关于html表单名称= php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆