php - 单击单选按钮后的操作

标签 php html mysql radio-button

我正在使用 PHP MYSQL 生成小型多页测验。我能够将问题和多项选择答案显示为单选按钮选项(感谢 Stackoverflow 的帮助)。我遇到的问题是——有没有办法在用户单击单选按钮时立即触发操作?如果用户选择了错误的答案,我想立即反馈并说明答案是错误的以及错误的原因。如果用户选择了正确答案,我想显示正确答案。

我查看了 $_GET、$_POST 和 $_REQUEST,但所有答案都需要“提交”才能在 PHP 中开始处理。我希望在用户单击单选按钮后立即开始操作(使用 PHP 源代码)。

注意:我在这里查看了几个似乎使用 jQuery 的问题。可以不用 jQuery 或 Javascript 吗?

感谢您的帮助。

最佳答案

是的,这是可能的,使用 jQuery 是最好的解决方案。

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>

关于php - 单击单选按钮后的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14400383/

相关文章:

php - 如何在提交表单后打印 woocommerce 通知

javascript - 尝试将元素动态添加到 <ul> 标记时,单击事件不起作用

mysql - 查询未按预期运行

c++ - MySQL, C++ - 以编程方式,MySQL Autoincrement 如何工作?

php 正则表达式最小和最大数字长度

php - 引号字符在数据库中的存储

php - codeigniter group by 不起作用

html - css 负背景位置或替代方案?

javascript - 如何在 thymeleaf 中传递名称以形成 Action ?

Php angularJS 项目未在简单的 html 表格中显示数据