dt614037527 2013-03-20 22:39
浏览 40

获得无线电组的价值

I am developing a page for multiple choice questions. User is only able to select an answer for each question. I can't seem to retrieve the value (1 indicates correct answer) of the radio buttons.

Here is structure of two tables that I use

CREATE TABLE IF NOT EXISTS `question` (
`q_id` int(10) NOT NULL AUTO_INCREMENT,
`q_qstn_no` int(11) NOT NULL,
`q_text` varchar(300) NOT NULL,
`q_chpt` int(11) NOT NULL,
PRIMARY KEY (`q_id`)
)
CREATE TABLE IF NOT EXISTS `answer` (
`a_id` int(6) NOT NULL AUTO_INCREMENT,
`q_id` int(10) NOT NULL,
`a_text` varchar(255) NOT NULL,
`a_value` tinyint(1) NOT NULL,
PRIMARY KEY (`a_id`)
)

HTML form containing the radio group.

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL ^ E_NOTICE);
    session_start();
    if(isset($_SESSION['tf1_sid']))
    {
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form id="form1" name="form1" method="post" action="ques_page_calc1.php">
    <p>Questions</p>
    <table width="900" border="0" cellpadding="4">
    <?php
    // db connect
    include("dbconn.php");
    // db query for questions
    $sql_q  = "SELECT q_id, q_qstn_no, q_text FROM question";
    $query_q = mysql_query($sql_q) or die("MySQL Error: " . mysql_error());
    // start loop for questions
    $rad2 = 0;
    while($data_q = mysql_fetch_array($query_q, MYSQL_ASSOC)){

    //        echo "<pre>";
    //        print_r($data_q);
    //        echo "</pre>";
    //        


        echo "<tr><td width='25' align='center' valign='top'><label><input name='q_no' size='1' type='hidden' value=". $data_q['q_qstn_no'] .">". $data_q['q_qstn_no'] ."</label></td>";
        echo "<td>". $data_q['q_text'] ."<br />";
        // db query for answers
        $sql_a = "SELECT a_id, a_text, a_value FROM answer WHERE q_id=".$data_q['q_id'];
        $query_a = mysql_query($sql_a) or die("MySQL Error: " . mysql_error());
        //$rad = 0;
        // start loop for answers
        while($data_a = mysql_fetch_array($query_a, MYSQL_ASSOC)){
            echo "<br /><table width='750' border='0'>";
            echo "<tr><td><label><input name='answer_".$rad2."' type='radio' value=". $data_a['a_value'] .">". $data_a['a_text'] . "</label></td></tr>";
            echo "</table>";
            //$rad++;
        }
        echo "</tr>";
        $rad2++;
    }
    echo "<tr><td><input name='Submit' type='submit' onClick='ques_page_calc1.php' value='Submit'></td></tr>";

    mysql_free_result($query_q);
    mysql_free_result($query_a);
    include("dbconn.php");

    ?>
    </table>
    </form>
    </body>
    </html>
    <?php
    }
    else
    {
    header("Location:s_login.php");
    }
    ?>

The PHP file to get the value selected

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL ^ E_NOTICE);
    // include db connection file
    include("dbconn.php");
    session_start();

    if(isset($_POST['Submit']))
    {   
$id = $_SESSION['tf1_sid'];
echo $id;
$correct = 0;
$ev_id = 1;

//db query to obtain i_id
$sql_i = "SELECT i_id FROM ins_stud WHERE s_id = '$id'";
$query_i = mysql_query($sql_i) or die("MySQL Error: " . mysql_error());
$data_i = mysql_fetch_array($query_i, MYSQL_ASSOC);
print_r($data_i);

// capture values from HTML form
if(!empty($_POST['answer_'.$rad2]))
{   
    foreach(($_POST['answer_'.$rad2]) as $ans)
    {
    echo $ans;
    var_dump($_POST);

    if($ans == 1)
        $correct = $correct + 1;
    }

    //echo $correct;

    // insert answer to table
    //$sql_eval = "INSERT INTO eval_set (ev_id, q_id, response, created) VALUES ('" . $ev_id . "', '" . $ques_no . "', '" . $ans . "', CURDATE())";
    //mysql_query($sql_eval) or die ("Error: " . mysql_error());
    //}
    }
    //  
    // insert result to table
    //$sql_result = "INSERT INTO result (r_score, ev_id, s_id, i_id) VALUES ('" . $correct . "', '" . $ev_id . "',  '" . $id . "',  '" . $data_i . "')";
    //mysql_query($sql_result) or die ("Error: " . mysql_error());

    //echo "Result: " . $correct . " questions correct.";   

    //header("Location:ass_result.php"); 
    }

    // close db connection
    mysql_close($dbconn);
    ?>

I thought it was the $_POST['answer_'.$rad2] that was causing the problem since I wasn't sure how to concatenate variables to name field. But now that's changed, there is still no output beyond print_r($data_i); line in the PHP file.

  • 写回答

2条回答 默认 最新

  • douchi5822 2013-03-20 22:44
    关注

    You don't need to give your radio group buttons different names. All of your choices will have a single name (say 'answer') and your PHP script will simply check for

    $_POST['answer']
    

    this will give you the selected radio button's value. So however many radio buttons you have for a certain question, give all of them the same name and you're fine. This will also make sure that only one of the radio buttons related to each other can be checked.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?