Jump to content

PHP Quiz


ItsWesYo

Recommended Posts

I'm assuming you know basic PHP, if that be the case, then you should be able to understand and modify this. I'm using 2 questions for an example.

[code]
<html>
<head>
<title>PHP Quiz</title>
</head>
<body>
<?php
//Check to see if null values were submitted...
if($_POST['question1'] == "" || $_POST['question2'] == "") {
?>
<form action="quiz.php" method="POST">
<b>Is php a programming language?</b><br>
<select name="question1">
<option value="" selected>Select An Answer</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<b>Ok, now, is C?</b><br>
<select name="question2">
<option value="" selected>Select An Answer</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<input type="submit" value="Submit Questions!">
</form>
<?php
} else {
//Variables, $x records questions correct. $y is the number of questions.
$x = 0;
$y = 2;
//Check the values of the select questions.
if($_POST['question1'] == "yes") {
$x++;
}
if($_POST['question2'] == "yes") {
$x++;
}
?>
<!--Some random HTML can go here...such as styling the page, leave the php inbetween these two comments-->
<?php
echo 'You got '. $x .' out of '. $y .' questions correct.';
?>
<!--Some random HTML can go here...such as styling the page-->
<?php
}
?>
</body>
</html>
[/code]

Note: This should be stored in a file named quiz.php, unless the form action is changed.
Link to comment
https://forums.phpfreaks.com/topic/15376-php-quiz/#findComment-62332
Share on other sites

Change this:
[code=php:0]<?php
echo 'You got '. $x .' out of '. $y .' questions correct.';
?>[/code]

To:
[code=php:0]<?php
if($x<6){$msg="You need to study more.";}
if($x>5 && $x<10){$msg="Nice, but you could do better.";}
if($x==10){$msg="Perfect!";}
echo 'You got '. $x .' out of '. $y .' questions correct. '.$msg;
?>[/code]
[hr]

Orio.
Link to comment
https://forums.phpfreaks.com/topic/15376-php-quiz/#findComment-62496
Share on other sites

Thanks again ...

Sorry for these questions ... but I have 1 more. I didn't think of the last two when I was posting this ...

Would there be a way to tell the person which questions they got right or wrong? Example:

Questions they answer:
1) What's my name?
2) What's my age?

Aftermath:
You got 1 out of 2 questions right.

Question 1: RIGHT
Question 2: WRONG

Link to comment
https://forums.phpfreaks.com/topic/15376-php-quiz/#findComment-62503
Share on other sites

Yes. Here is the script...

[code]
<html>
<head>
<title>PHP Quiz</title>
</head>
<body>
<?php
//Check to see if null values were submitted...
if($_POST['question1'] == "" || $_POST['question2'] == "") {
?>
<form action="quiz.php" method="POST">
<b>Is php a programming language?</b><br>
<select name="question1">
<option value="" selected>Select An Answer</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<b>Ok, now, is C?</b><br>
<select name="question2">
<option value="" selected>Select An Answer</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<input type="submit" value="Submit Questions!">
</form>
<?php
} else {
/*Variables, $x records questions correct. $y is the number of questions. $z is ammount of questions wrong.*/
$x = 0;
$y = 2;
$z = 0;
//Check the values of the select questions.
if($_POST['question1'] == "yes") {
$x++;
$q1is = "<br />Question 1: RIGHT";
} else {
$q1is = "<br />Question 1: WRONG";
}
if($_POST['question2'] == "yes") {
$x++;
$q2is = "<br />Question 2: RIGHT";
} else {
$q2is = "<br />Question 2: WRONG";
}
?>
<!--Some random HTML can go here...such as styling the page, leave the php inbetween these two comments-->
<?php
if($x<1){$msg="You need to study more.";}
if($x>0 && $x<2){$msg="Nice, but you could do better.";}
if($x==2){$msg="Perfect!";}
echo 'You got '. $x .' out of '. $y .' questions correct. '.$msg;
echo $q1is;
echo $q2is;
?>
<!--Some random HTML can go here...such as styling the page-->
<?php
}
?>
</body>
</html>
[/code]

That should output what you are asking.
Link to comment
https://forums.phpfreaks.com/topic/15376-php-quiz/#findComment-63160
Share on other sites

Archived

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

×
×
  • 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.