Jump to content

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

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.