Jump to content

Processing a Quiz Using PHP


jimmyo88

Recommended Posts

Hello everyone, I have a datatbase containing fields id, question, answer, and type. The questions are in true/false format, multiple choice and fill in the blank(text). At the moment, I am only trying to get the true and false questions to work. My code is as follows

 


<?php

$connection = mysql_connect("localhost","root","root") or die("couldn't connect");
$select = mysql_select_db("login") or die ("cannot select database!");

$query = mysql_query("SELECT * FROM questions");

$numrows = mysql_num_rows($query);

if ($numrows!=0)
{
//code to login
while($row = mysql_fetch_assoc($query))
{
$dbquestion = $row['question'];
$dbanswer = $row['answer'];
$dbtype = $row['type'];
$dbid = $row['id'];
$correctanswer =  explode('|',"$dbanswer");



switch($dbtype){
case "mchoice":
echo"<br/>"; echo $dbquestion; echo"<br/>";
break;

case "boolean":
echo"<br/>"; echo $dbquestion; echo"<br/>";
echo $correctanswer[0];

//display radio buttons

?> 
<form name="boolean" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
True
<input name="question" type="radio" value= "True" /> 
False 
<input name="question" type="radio" value= "False" />
<input name="id" input type="hidden" value="<?php $dbid ?>" />
<input type="submit" name="submit" id="submit" value="Submit" />

<?php
echo "The answer submitted was ";
echo  $_POST['question'];
?>

<?php
break;

case "text":
echo"<br/>"; echo $dbquestion; echo"<br/>";
//display text box
break;
}
}
}
?>

 

The problem I'm having is that this creates a submit button for each question. When choosing true or false for the answer and pressing submit, the answer is selected for every question. I need the code  to look at what the user has entered, check the answer against correctanswer[0].

 

I'm really in a bit of a hole here and if anyone can help me it would be greatly appreciated.

 

Many thanks in advance

James

Link to comment
Share on other sites

thanks a lot for the reply. Could you elaborate a bit more, I do not have $_POST['id'] in the script. I have a variable $dbid which contains the question number.

Is this what i'm doing wrong? It's now 6am here, I think I may have to call it a night soon. I've been trying to get this working for about 10hours!!

Link to comment
Share on other sites

Ok, in the form you have this:

 

<input name="id" input type="hidden" value="<?php $dbid ?>" />

 

That creates a hidden input in your form with name "id" and value from $dbid.

 

When the form is submitted using post, that same value can be used in your script as $_POST['id'].  So if you use that id when you update the database, you should find that you only update the answer for that one question.

 

The other thing you need to do is specify which radio button is selected when printing out the radio buttons, using "checked".  An example is here: http://www.echoecho.com/htmlforms10.htm

 

In your php you'll need to check if that question is currently answered true or false, and only put "checked" on the corresponding input tag.

Link to comment
Share on other sites

Thanks for that, I looked in to the checked option so that all the options are set to true initially.

 

I dont actually need to update the database with the answer. What i need the script to do is to add up all the correct answers and then give the total score.

Whats giving me the problem is the switch statement. I can't seem to store the $_POST answer individually for each question.

 

E.g. when i click submit, the code which shows me what values were posted

<?phpecho "The answer submitted was ";echo  $_POST['question'];?>

 

the echo  $_POST['question'] variable is the same for each question. So all questions are either true or false.

Also because the submit button is part of the switch statement, this also gets echo'd out for each question.

 

I've been looking at this same piece of code for about 6hrs now! I just want to move ahead with my project!

 

thanks for taking the time with this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.