0

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

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

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

谢谢你的帮助。

4

5 回答 5

3

是的,这是可能的,使用 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>
于 2013-01-18T14:11:53.187 回答
1

试试这个

$("input[type='radio'].your_radio_class").bind("change click", function(){
    //  stopping user from changing the answer further depends on ui specification
    //  disabling radio
    $("input[type='radio'].your_radio_class").each(function(){ 
       $(this).attr({"disabled":true,"readonly":"readoly"});
    });
    // else  show loading graphics
    $(this).parent().html("Loading answer...wait");
    //  make ajax call
    //  fetch answer
    //  display answer
});

于 2013-01-18T14:01:38.053 回答
0

您必须考虑的是 PHP 在服务器端运行的事实。话虽如此,您只能通过将请求发送到服务器来处理。这是“提交”操作或 JQuery/JavaScript/Ajax 调用,它们在本地运行,向服务器发出请求,然后更新页面。因此,要使用 PHP,您必须提交或使用本地脚本进行调用。

于 2013-01-18T13:57:30.477 回答
0

这最好用 javascipt 来完成(jQuery 对你来说可能更容易)。

第一:你设置了一个点击事件,当一个单选按钮被点击时会触发一个动作:http:
//api.jquery.com/click/

第二:当点击被触发时,你设置了一个对 php 文件的 ajax 调用,它验证输入并返回结果:
http ://api.jquery.com/jQuery.ajax/

剩下的就看你了。

于 2013-01-18T13:58:17.437 回答
0

如果您想在用户实际点击提交按钮之前验证用户的选择,您需要使用 onchange 属性为您要验证的单选按钮启动一个 ajax 函数(带参数),然后 ajax 将获得响应从服务器又名 php 代码验证用户的选择并返回结果。(如果你做对了)

于 2013-01-18T14:00:12.463 回答