Checking Correct Answer and Submitting Form

2019-07-28 04:22发布

问题:

I am admittedly not as skilled in PHP as I would hope to be. Most of my experience is with Wordpress loops.

I'm trying to create a very simple quiz that carries the number of correct answers in the URL (eg. domaindotcom/?p=3 if they've got 3 correct answers so far).

I'm using the following PHP code to start it off:

<?php 
    /* Gets current correct answer Count */ 
    $answer_count = $_GET["p"]; 

    /* checks to see if the submitted answer is the same as the correct answer */
    if ($_POST["submitted-answer"] == "correct-answer") {
        $answer_count++;
    }
?>

Now I know I can get the correct link by using the link:

<a href="link-to-next-question.php/?p=<?php echo $answer_count; ?>">Next Question</a>

But now I'm trying to use it in a form and confused by POST, GET, etc.

Here's my HTML:

<form name="quiz" action="" method="POST">
<label for="o1"><input type="radio" name="grp" id="o1" value="o1"> Label 1</label>
<label for="o2"><input type="radio" name="grp" id="o2" value="o2"> Label 2</label>
<label for="o3"><input type="radio" name="grp" id="o3" value="o3"> Label 3</label>
<input type="submit" value="Next Question" class="btn">
</form>

How can I choose the correct answer (security not important, it's just a fun quiz) and then send them along to the next URL while adding the increment to the $answer_count before the URL is created?

回答1:

Don't pass the answer count in the link (i.e. by GET). Instead just include a hidden form field(s), and use client side javascript code to increment variables you want and submit the form.

<form id="qfrm" name="quiz" action="" method="POST"> 
  <input type="hidden" name="question_number" value="<?php echo $next_question_number?>">
  <input type="hidden" id="n_c_a" name="num_correct_answers" value=<?php echo $num_correct_answers?>">
  <input type="button" value="Submit" onclick="validateAnswerAndSubmit(); return false;">
</form>

<script language="javascript">
  function validateAnswerAndSubmit(){
    if(validate_answer()){
      document.getElementById("n_c_a").value += 1;
    }
    document.getElementById("qfrm").submit();
  }  
</script>

Then just make your php script switch on $_POST["question_number"]


OK so you don't want to use javascript... if you're really asking "how do I know which radio box was selected from PHP?" the answer is:

<?php $answer = $_POST["grp"]; ?>

I think you should really be passing two variables in the URL, one would be the question_number, and the other would be num_correct. You could then write code like this:

<?php

$num_correct = (int) $_GET["num_correct"];
$question_number = (int) $_GET["question_number"];

switch($question_number - 1){ // assuming your question numbers are in order by convention
                              // validate the answer to the previous question
  case -1: //no validation necessary for the first question ($question_number 0)    
    break;
  case 0: 
    if($_POST["grp"] == "correct answer"){
      $num_correct++;
    }
    break;

   // and so forth;         
}       
?>

<form name="quiz" 
      action="this_page.php/?num_correct=<?php echo $num_correct;?>&question_number=<?php echo $question_number + 1?>" 
      method="POST">

<?php Display_Question_Number($question_number);?>

</form>

The key point here is that "action=" in the form is analogous to "href=" in the anchor, i.e. it's the URL to which the form is submitted when the user clicks the submit button.



回答2:

Use a type="hidden" datafield to sent the current count.



回答3:

You can set the count of correct answers in $_SESSION (it's a global variable that persists between pages), so it's harder to cheat.



标签: php forms post get