tags:

views:

71

answers:

3

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  A: 

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.

vicatcu
Everything from the client side can easily be tampered. A hidden field is as bad as passing it as URL param
Gordon
Thanks for your response. I appreciate your time writing all that up. I'm doing this for mobile browsers only, some of which don't support JavaScript, so I'm using just PHP/HTML/CSS.Tampering isn't an issue. This is a quiz just for fun, and if a user wants to cheat, so be it. It's just a requirement that I count the answers in URL.
Anders H
Is that using a value of "correct answer" on the right radio button? Or in other words, how is the answer validated. I understand the rest of your post. I've only got 10 questions, each with 3 options, so it would be pretty easy for me to hard-code the answers.
Anders H
The global $_POST variable will have a key corresponding to every named input field in the submitted form. So from your code you had named the radio button group "grp". The value of the $_POST["grp"] entry would be the value attribute of the selected radio button. So to be meaningful, "correct answer" in the code above would have to be one of "o1", "o2", or "o3". If you want to know what is in the $_POST variable put `<?php print_r($_POST);?>` at the top of yoru PHP, and you'll get a pretty printed output. I see no problem with hard coding the correct answers for your application.
vicatcu
+1  A: 

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

Afwas
Ok, this makes some sense. Can anyone elaborate a bit? I understand about hidden fields etc, but still don't understand how I can post data to the $answer_count variable, have it included in the URL and go to the next page.
Anders H
You change your `action=""` to page for the next question, so action could be `action="page9.php"`. In page9.php you get the current variable `$counter = $_POST["counter"];`, then check the answer and add one.That number will be the value for the hidden field in the question on page9.php `<input type="hidden" name="counter" value="$counter" />` and will be 'transported' to page 10.
Afwas
Thanks for your help. I understand now what you mean about the counter, and about the action for getting to the next page. Can you refer me to a tutorial or give me some guidance on checking the answer and adding the count to the $answer_count?
Anders H
just follow the pattern i describe below (edit from previous post) and you can do it all in one php page, with a bunch of "cases".
vicatcu
A: 

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.

Raveren
Cheating isn't an issue (see above comment). $_SESSION might help, but how can I both send the answer to $_SESSION and go on to the next page in 1 click?
Anders H