I'm a beginner in PHP and I've set up a test quiz in PHP. Below is the code for the html quiz:-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form method="POST" action="quizStore2.php">
<p>Please answer the following questions to take part in this quiz</p>
<fieldset>
<legend>Quiz</legend>
<ol>
<li>
Which web server software is thought to run over 65% of the world's websites?
<p><input type="radio" name="Q1" id="a1" value="Apache" /><label for="a1">Apache</label></p>
<p><input type="radio" name="Q1" id="b1" value="IIS" /><label for="b1">Microsoft IIS</label></p>
<p><input type="radio" name="Q1" id="c1" value="NCSA" /><label for="c1">NCSA</label></p>
<p><input type="radio" name="Q1" id="d1" value="IBM" /><label for="d1">IBM HTTP Server</label></p>
</li>
<li>
Which two of the following web standards are primarily co-ordinated by the W3C?
<p><input type="checkbox" name="Q2[]" id="a2" value="HTML" /><label for="a2">HTML</label></p>
<p><input type="checkbox" name="Q2[]" id="b2" value="ECMA" /><label for="b2">ECMAScript</label></p>
<p><input type="checkbox" name="Q2[]" id="c2" value="WML" /><label for="c2">WML</label></p>
<p><input type="checkbox" name="Q2[]" id="d2" value="DOM" /><label for="d2">DOM</label></p>
</li>
<li>Which of the following is not a type of CSS positioning?
<p>
<select name="Q3">
<option value="">Choose one</option>
<option value="Absolute">Absolute</option>
<option value="Static">Static</option>
<option value="Flexible">Flexible</option>
<option value="Fixed">Fixed</option>
</select>
</p>
</li>
<li>Which two of the following are block-level HTML elements?
<p>
<select name="Q4[]" multiple="multiple">
<option value="EM">EM</option>
<option value="H1">H1</option>
<option value="IMG">IMG</option>
<option value="DIV">DIV</option>
</select>
</p>
</li>
</ol>
</fieldset>
<p> </p>
<fieldset>
<legend>Your Details</legend>
<p>Your Name: <input type="text" name="userName" /></p>
<p>Email Address: <input type="text" name="userEmail" /></p>
</fieldset>
<p><input type="submit" value="Submit" /> or <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
and here is the PHP code:-
<?php
$counter=0;
$thisName=$_POST['userName'];
$thisEmail=$_POST['userEmail'];
$webSoft=$_POST['Q1'];
$webStand=implode('',$_POST['Q2']);
$webCSS=$_POST['Q3'];
$blockCSS=$_POST['Q4'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
showFormInput();
?>
</body>
</html>
<?php
function showFormInput()
{
global $webSoft, $webStand, $webCSS, $blockCSS, $thisName, $thisEmail, $counter;
Print <<<END
<h2>Your Response</h2>
<p>Your name: <b>$thisName</b></p>
<p>Your email: <b>$thisEmail</b></p>
<p>Your answers: Q1:<b>$webSoft</b><br />
Q2:<b>$webStand</b><br />
Q3:<b>$webCSS</b><br />
Q4:<b>$blockCSS</b><br />
</p>
<hr />
END;
//Qu1
if ($webSoft=="Apache") $counter++;
//Qu2
$qTwo = array("H1","WML");
for($i=0;$i<sizeof($qTwo);$i++)
$counter++;
//Qu3
if ($webCSS=="Flexible") $counter++;
//Qu4
//$arr=array("H1","DIV")
//for($i=0;$i<sizeof($arr);$i++)
//if(substr_count($blockcss, $arr[$i])) $counter++;
echo $counter." ";
}
?>
The output for the quiz choices works OK, but I'm having problems getting the counter to increment correctly for Q2. If I get this to work, then Q4 will work. Qu1 is below:
if ($webSoft=="Apache") $counter++;
and Qu3
if ($webCSS=="Flexible") $counter++;
work fine and if the right answer is selected, the $counter is incremented correctly. I'm stuck on Qu2 which requires the user to make two correct choices and then the counter is incremented for each correct answer
$qTwo = array("H1","WML");
for($i=0;$i<sizeof($qTwo);$i++)
$counter++;
What's happening is that the counter is incremented twice, irrespective of whether a selection is made or whether it is the correct answer. If someone could help, I would appreciate it.
Many thanks