douzhuo2722 2011-10-15 08:18
浏览 35
已采纳

如何增加变量,并在一系列页面上记录单选按钮的值?

I am making a basic quiz program, that when finished will allow the user to complete quizzes being read from a MySQL database, and write quizzes to be stored in the database.

I have a variable for question number ($qn), which when incremented changes the question, and the four possible choices to the next question in the database.

My question is: How can I have a button on my main form, that (once the user has selected a radio button) recordes the radio button result, then increments $qn to display the next question, then does that process again, until $qn == $numberOfQuestions, at that point it needs to show the page results.php and add up the results from all the radio buttons on the quiz.php page.

I am quite new to PHP, so sorry if this is a particularly stupid question, or if I have worded it in an unclear way. And I am very open to suggestions is there is an easier or more efficient way of doing this.

Thank you in advance for any answers, really grateful :)

  • 写回答

2条回答 默认 最新

  • dsfo22654 2011-10-15 11:13
    关注

    You just process the forms input, store the value and continue to the next question.

    A rudimentary example (Demo):

    <h1>Quiz</h1>
    <?php
    $quiz = load_quiz();
    $states = isset($_GET['state']) ? explode(',', $_GET['state']) : array();
    $states = array_map('intval', $states);
    $page = count($states)+1;
    
    if (isset($_POST['answer']))
    {
         $states[$page-1] = (int) $_POST['answer'];
    }
    
    if ($page > count($quiz))
    {
    ?>
    <h2>Your selection:</h2>
    <?php
    foreach($quiz as $key => $question)
    {    
        $state = $states[$key];
        $answer = $question['answers'][$state];
        printf('%s: %s<br />', $question['question'], $answer);
    }
    
    }
    else
    {
    ?>
    <h2><?php echo $quiz[$page]['question']; ?></h2>
    <form method="post" action="?state=<?php echo implode(',', $states); ?>">
    <?php
    foreach($quiz[$page]['answers'] as $key => $answer)
    {
        printf(
            '<input type="radio" name="answer" id="answer-%d" value="%d"><label for="answer-%d">%s<label><br>',
            $key, $key, $key, $answer
        );
    }
    ?>
    <input type="submit" value="Next">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?