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?
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.
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:
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:
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.
Use a type="hidden" datafield to sent the current count.
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.