开发者

Using PHP IF ELSE for online treasure hunt

开发者 https://www.devze.com 2023-04-12 12:24 出处:网络
I\'m trying to create an online treasure hunt game where users have to go to specific sites to get the clues and enter it on a website. There are 7 questions whereby upon getting a correct answer, the

I'm trying to create an online treasure hunt game where users have to go to specific sites to get the clues and enter it on a website. There are 7 questions whereby upon getting a correct answer, the user will see the next question and clue.

So far this is my PHP code.

&开发者_JS百科lt;?php
//task 1
if(strtolower($_POST["task1"]) == "hello") {
?>
Here's your next clue!
<p><form action="quiz2.php" method="post">
<input type="text" name="task2" />
<input type="submit" />
</form>
<?php
} else {
?>
Wrong!
<p><form action="quiz.php" method="post">
<input type="text" name="task1" />
<input type="submit" />
</form>
<?php
}
?>

I know how to do it in multiple pages but I would like it if I can do it in one PHP page itself. The reason being, users can just view source the page and just skip to the next question/clue without answering.

How do I achieve this in one PHP page?


There are a lot of ways to handle this. My preference?

  1. Allow users to define a unique id (fun name or something) to track progress.

  2. Have a small db store ids and progress.

  3. Use AJAX to post and retrieve data.

This approach would require one HTML page with AJAX methods, one PHP controller that accepts the unique id and and answer. If the answer is correct, the PHP controller records the status to the DB allowing the controller to return the next clue. If a user logs off the page and comes back, they just have to put in their ID and pick up where they left off. This is a fine solution if security isn't an issue, and it doesn't sound like it is.

Happy coding!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号