tags:

views:

102

answers:

3

I need to create a 10 page quiz for a mobile browser. It is only a mobile webpage, so no considerations need to be taken for other browsers.

Here's the problem I'm having: I can't use JavaScript, because not every mobile browser supports it. I'm not very skilled in other languages, but I thought perhaps something could be done in PHP as it is server-side.

If my first URL is domain and I enter the correct quiz answer, the URL to the next page could be domain/?p=1. The URL doesn't need to do anything but hold a count of the number of correct results.

As for the actual code, I was thinking it could be included in the HTML itself, as I'm not very concerned about people viewing the source on their mobile phones.

Is it possible to write a line of code that increments the 'p=' attribute in the URL by one when clicked and only attach it to the correct answers?

Here's an image of what I mean: http://i.imgur.com/HbJ5U.jpg

+6  A: 

And, what's to stop me from manually incrementing the "correct answer" counter in my address bar?

Do you not want to use a database because you don't have one available to you in your hosting, or because you don't know how?

I'm not a fan of the idea, but you can get the number of "correct answers" with the following code.

<?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, you just add the modified answer count to the link to the next question.

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

If this is "just for fun" I don't see why you couldn't do it like this. It's definitely a simple way to solve the problem.

Robert Greiner
If you manually increment the correct answer, it doesn't matter. It's a quiz for fun and if people want to cheat to make themselves feel smarter, by all means let them. I actually think most of the general public won't pick up on the correlation.I don't want to use a database, because I don't personally care about the results. The results are given to the user, and that's it. No saving or anything.
Anders H
Your example seems simple enough. Even I can understand that simple of PHP. With your code, how are you defining which answer is correct? In a form?
Anders H
cool, I'm glad this is what you needed. The code above assumes the form has been submitted by the user and the answer has been checked for correctness (so, the $correct variable will be true if the answer is correct). Check my final edit inside the if() condition to check for correctness.
Robert Greiner
+2  A: 

The standard way to do this is to store things in hidden form variables. Of course, if there is anything riding on this, that's a terrible way to do it, because it's really easy for the end user to put his own values in those hidden form values.

Paul Tomblin
Nothing riding on it. It's just a fun quiz for people. If they want to cheat, let them. I understand hidden values in forms etc, but how would I append that to the URL?
Anders H
@Anders, You wouldn't append it to the URL. You'd use POST.
Paul Tomblin
A: 

Aren't file-based sessions the obvious answer here?

ZombieSheep
Possibly, put I'm primarily a designer. I'm not looking for someone to do the work for me, but just point me in the right direction as my brain doesn't even know where to start the research. Checking out file-based sessions now...
Anders H
sessions are bad for such kind of apps; as they die after 20 minutes;
effkay
@effkay - Sorry, but that sounds like a very naive and ill-considered answer to me. First of all, sessions don't have to die after 20 minutes, it depends how they are configured. Second, assuming that you were correct, if your users are taking 20 minutes per question, they are probably taking this "fun quiz" far too seriously.
ZombieSheep
@Zombie - it is a matter of personal choice. Yes, 20 minutes per question are enough: but I still will prefer mechanisms other then session: considering the principal of application being scalable by design; Like again, it is matter of personal choice.
effkay