dongyu8664 2013-01-18 13:53
浏览 65
已采纳

单击单选按钮后的操作

I am using PHP MYSQL to generate a small multi page quiz. I am able to display the questions and the multiple choice answers as radio button choices(thanks to help from Stackoverflow). The problem I am running into is - is there a way to trigger an action as soon as a user clicks on a radio button? If the user has selected a wrong answer, I want to give immediate feedback and say the answer is wrong and why it is wrong. If the user has selected a correct answer, I want to display correct answer.

I have looked at $_GET,$_POST and $_REQUEST but all require the answers to be "Submit"ted for the process to begin in PHP. I want the action to begin (using PHP source code) as soon as the user clicks a radio button.

Note: I have looked at several questions here that seem to utilize jQuery for the above. Is it possible to do without jQuery or Javascript?

Thanks for your help.

  • 写回答

5条回答 默认 最新

  • duande1985 2013-01-18 14:11
    关注

    Yes it is possible and to use jQuery is the best solution.

    http://api.jquery.com/jQuery.ajax/

    <?PHP
    if (!empty($_GET['action']) && $_GET['action'] == 'ajax_check') {
      // I a final solution you should load here the answer of the question from database by name
      $answers = array(
        'foo' => 1,
        'baa' => 3,
      );    
    
      // Prepare and default answer in error case
      $return = array(
        'message' => 'Invalud choise',
      );
    
      if (isset($answers[$_GET['name']])) {
        // If question/answer was found in database, check if users chouse is correct
        if ($answers[$_GET['name']] == $_GET['value']) {
          $return['message'] = 'Correct answer'; 
        } else {
          $return['message'] = 'Wrong answer'; 
        }
      }
    
      // Return answer to java script
      header('Content-type: text/json');
      echo json_encode($return);
      die();
    }
    ?>
    
    Question 1
    <input type="radio" name="foo" value="1" class="question_radio" />
    <input type="radio" name="foo" value="2" class="question_radio" />
    <input type="radio" name="foo" value="3" class="question_radio" />
    <input type="radio" name="foo" value="4" class="question_radio" />
    <br />
    
    Question 2
    <input type="radio" name="baa" value="1" class="question_radio" />
    <input type="radio" name="baa" value="2" class="question_radio" />
    <input type="radio" name="baa" value="3" class="question_radio" />
    <input type="radio" name="baa" value="4" class="question_radio" />
    
    <!-- Load jquery framework from google, dont need to host it by your self -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function () { 
        // When document was loadet succesfull
    
        $(".question_radio").click(function () {
          // Listen on all click events of all objects with class="question_radio"
          // It is also possible to listen and input[type=radio] but this can produce false possitives.
    
          // Start communication to PHP
          $.ajax({
            url: "test.php", // Name of PHP script
            type: "GET",
            dataType: "json",  // Enconding of return values ²see json_encode()
            data: { // Payload of request
              action: 'ajax_check', // Tel PHP what action we like to process
              name: $(this).attr('name'),
              value: $(this).val(), 
            }
          }).done(function(data) {
            // Procces the answer from PHP
            alert( data.message );
          });
        });
      });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了