Jump to content

Multiple Values in Exam


VanKaizer

Recommended Posts

I have just started to learn PHP and Javascript in the past week. I often find it best to learn these things through actually working on a small project and trial/error. To begin my learning process I have set out creating an exam that has more than one value as the correct answer. Here is the code for the question itself:

What devices are available to borrow?<form action="answer.php" method="POST"><br /><input type="text" maxlength="22" name="key" /><input type="hidden" name="correctkey" value="laptop" /><input type="hidden" name="page" value="question1.PHP" /><input type="hidden" name="nextpage" value="question2.PHP" /></form>

Here is what is looks like server side:

<? ob_start(); ?><html><?php$key = $_POST['key'];$correctkey = $_POST['correctkey'];$page = $_POST['page'];$nextpage = $_POST['nextpage'];if ($key==$correctkey)  header("Location: $nextpage");else  header("Location: $page");?></html><? ob_flush(); ?>

In this case "laptop" is a correct answer, but I want to make it so more than one item could be considered correct. Help a noob coder out. Thanks guys!

Link to comment
Share on other sites

you can use array to hold the correct answers. and then check against that array with the use input if matches it is right else wroonghttp://php.net/in_array

Link to comment
Share on other sites

I realize you're doing it for a trial&error purposes, but you may also want to know some security tips early...Everything on the client - whether it's HTML code, CSS or JavaScript - could be viewed by a user, if they were wise enough to right click and select "View Source". If you were doing this "exam" for a student, chances are they'll know (or learn from a fellow student) how to do that, and essentially cheat.It's a better idea to simply have the form submit the answer and a question number (or, as you do, the question page), and let the server check whether the answer is right according to its own criteria.As for your original question about specifying multiple right answers... that depends on how you choose to change store the right answers.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...