dqlhsm9820 2012-07-09 15:19
浏览 241
已采纳

PHP中的多项选择

How can i save all the inputs(question, choices, answers) in the database? I cant save the correct answer on the database. how can i get the chosen value, which is selected through the radio button? Or is there any other?

<form action="test2.php" method="post">
<?php
if($numMC > 0)
    {

echo "<b>"."MULTIPLE CHOICE QUESTIONS: Enter them below followed by their correct answer."."</b>";
        echo "<br>"; 

        for ($j=1; $j<=$numMC; $j++)
            {?> 
<p><textarea name="question[<?php echo $j; ?>]" rows=3 cols=90>question[<?php echo $j; ?>]</textarea></p>
            <?php for($k=1;$k<=$numchoices;$k++)
                {
                echo $k; ?>
                <input type="radio" name="right[<?php echo $j; ?>]">
                <input type="text" name="choice[<?php echo $j; ?>][<?php echo $k; ?>]" value="choice[<?php echo $j; ?>][<?php echo $k; ?>]"><br />
<?php               }

                echo "<br>"."<br>";
            }

    } ?>

<input type="submit">     

how will i determine which radio button is selected and how will i get the value of the radio button that is selected?

  • 写回答

1条回答 默认 最新

  • douxian4376 2012-07-09 15:22
    关注

    You can create a table with questions like so:

    ----------------------------------------------------
    | questionId | answer                    | correct |
    ----------------------------------------------------
    | 1          | Possible answer 1a        | 0       |
    ----------------------------------------------------
    | 1          | Possible answer 1b        | 1       |
    ----------------------------------------------------
    | 2          | Possible answer 2a        | 1       |
    ----------------------------------------------------
    | 2          | Possible answer 2b        | 0       |
    ----------------------------------------------------
    

    You can build the page like this:

    $sql = mysql_query("SELECT * FROM questionsTable");
    while($row = mysql_fetch_assoc($sql)){
    echo '<input type="radio" value="'.$row['answer'].'" name="q'.$row['id'].'"/>
    }
    

    You can check if the answers are correct after the form has been submitted like this:

    $sql = mysql_query("SELECT * FROM questions");
    while($row = mysql_fetch_assoc($sql)){
        $radioName = 'q'.$row['id'];
        if($row['correct'] == 1){
            //handle a correct answer here
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路