duanhanglekr37902 2015-11-28 08:31
浏览 33
已采纳

测验如何提交答案并对其进行评分

I have 10 question at a time on a page quiz.html- i.e

question 1: what is your name?

<option value="a">a</option>   
<option va..>b</option>...    
<option va...>e</option>

question 2: what are the colors in usa flag?

<option>a</option> 

and so on.

My question is how do i submit this question at a time and add +1 into the score field in the database for each correct answer?

I can do the saving and scoring if there is only 1 question per page i.e

select * from quiz where id='qid'

then

while($fetch=mysql_fetch_row($query)){
    if( $fetch["answer"]==$useranswer ){ add +1 to his score; }
}

But for many question i don't know what to do.

The structure of the html is:

<form action="quizer.php" method="post">
    <input type="hidden" name="q1" value="qid"> what is your name? 
    <option value="a">rolex</option>
    <option value="b">wales</option>
    <option value="c">israel</option>
    <option value="d">ade</option>

    <br>
    <input type="hidden" name="q2" value="qid"> 
    is nigeria a country?
    <option value="a">no</option>
    <option value="b">yes</option>

    and so on... 
    <input type="submit">
</form> 
  • 写回答

1条回答 默认 最新

  • douqi2571 2015-11-28 09:38
    关注

    Assuming that the questions are using select menus as you sort of suggested then this should help - though it is not tested...

    <?php
        /* quizer.php */
        if( $_SERVER['REQUEST_METHOD']=='POST' ){
            /* select correct answers from db */
            $sql='select * from `quiz`';
            $res=mysql_query( $sql, $con );
    
            /* store answers in an array for marking */
            $answers=array();
            $score=0;
            $index=0;
    
            /* populate the answers array */
            while( $rs=mysql_fetch_object( $res ) ){
                $answers[]=$rs->correct_answer;
            }
    
            /* mark the answers */
            foreach( $_POST as $question => $answer ){
                echo $question.' '.$answer;
                if( $answer === $answers[ $index ] ) $score++;
                $index++;
            }
            /* update the db */
            $sql="update `user` set `score`=$score where `username`='$username';"; /* etc */
        }
    ?>
    
    
    
    <form action="quizer.php" method="post">
        <!-- assume select menus for questions and possible answers -->
        <label for='q1'>what is your name? 
            <select name='q1'>
                <option value="a">rolex</option>
                <option value="b">wales</option>
                <option value="c">israel</option>
                <option value="d">ade</option>
            </select>
        </label>
    
        <br />
    
        <label for='q2'>is nigeria a country?
            <select name='q2'>
                <option value="a">no</option>
                <option value="b">yes</option>
            </select>
        </label>
    
        <br />
    
        <input type="submit" value='Submit Quiz'>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示