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 の方が簡単かもしれません)。

1つ目:ラジオボタンがクリックされたときにアクションをトリガーするクリックイベントを設定します:
http://api.jquery.com/click/

2番目: クリックがトリガーされると、入力を検証して結果を返す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 に答える