Hi,

I am trying to do true false quetions in php. How do I score the result.

Please help.

-------

<form action="result.php" method="post">

<p>List of True or False questions. </p>




<table width="100%" cellspacing="0" cellpadding="0" border="0">

<input value="1" name="Question1" id="Question1" type="hidden">
<input value="2" name="NoOfResponse1" id="NoOfResponse1" type="hidden">


<tr height="35px">

<td id="quest1" style=" width="2%" colspan="2">1</td>

<td></td>


<td valign="top" colspan="2">


<br> 2 + 2 equal 6.<a name="quest1"></a><br>

</td></tr>


<tr >
<td colspan="2"></td>
<td id="LID1_1"></td>

<td ><nobr>

<input value="LID1_1" name="Answer1" type="radio"><b>A)</b>
<img height="1" width="10" src="...gif"></nobr></td>


<td width="100%">TRUE</td></tr>

<tr >
<td colspan="2"></td>
<td id="LID1_2"></td>

<td style="text-align:right;" valign="top"><nobr>
<input value="LID1_2" name="Answer1" type="radio"><b>B)</b><img height="1" width="10" src=""></nobr></td>

<td width="100%">FALSE</td></tr>













<input value="2" name="Question2" id="Question2" type="hidden">
<input value="2" name="NoOfResponse2" id="NoOfResponse2" type="hidden">
<tr><td bgcolor="#CCCCCC" colspan="5"><img height="1" width="1" src=""></td></tr>

<tr height="35px"><td id="quest2" style=" width="2%" colspan="2">1</td>

<td><img height="1" width="5" src=""></td>
<td valign="top" colspan="2"><img height="5" width="1" src="">
<br>2 - 2 equal 0<a name="quest2"></a><br>

</td>


</tr>

<tr height="25px">
<td colspan="2"></td>
<td id="LID2_1"><img height="1" width="40" src=""></td>
<td >

<nobr>

<input value="LID2_1" name="Answer2" type="radio"><b>A)</b>
<img height="1" width="10" src=""></nobr></td>
<td width="100%">TRUE</td></tr>


<tr height="25px"><td colspan="2"></td>
<td id="LID2_2"><img height="1" width="40" src=""></td>

<td style="text-align:right;" valign="top">

<nobr>
<input value="LID2_2" name="Answer2" type="radio"><b>B)</b>
<img height="1" width="10" src="/"></nobr></td>

<td width="100%">FALSE</td></tr>

<input type="submit">
</form>

Recommended Answers

All 3 Replies

Once you submit that form to PHP all your radio box values will be available in the $_POST array by their name (i.e. $_POST['Answer1'] would have a value of LID1_1 or LID1_2 depending on which answer was selected. From there you just need to do a conditional if statement if($user_answer = $correct_answer)

Member Avatar for Zagga

To expand a little on GliderPilots excellent answer ...

<?php
// Set default values.
$score = 0;
$tried_answer1 = "";
$tried_answer2 = "";
$correct_answer1 = "LID1_2";
$correct_answer1 = "LID2_1";

// Get POST values.
$tried_answer1 = $_POST['Answer1'];
$tried_answer2 = $_POST['Answer2'];

// Compare answers.
if ($tried_answer1 == $correct_answer1){
    $score += 1; // Add score
}
if ($tried_answer2 == $correct_answer2){
    $score += 1;
}

// Display score.
echo "Score: " . $score;
?>

Line 7 above should be:

$correct_answer2 = "LID2_1";

commented: Well spotted, my bad +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.