简体   繁体   中英

Storing customisable quiz questions and answers in a .txt file using PHP

I have a customisable quiz site, where the user can input questions and answers. My first PHP file takes the inputted information and generates a quiz page. The answers are in hidden text box. A second PHP then compares the inputted radio button to the hidden text box to see if the user is correct and gives a score in relation to that. I would like to store the questions and answers in a .txt file which could then be accessed by other users, unfortunately as part of the project we are not able to use MySQL. Hopefully I am being clear, I have tried to go about this many ways but all have broken my code. Any ideas would be much appreciated.

My code is below:

INITIAL HTML:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>


</head>

<body>

<form action="actions.php" method="post">
<strong>Question 1:</strong> <input name="q1" type="text" /> <br />
Answer 1: <br />
<input name="a11" type="text" /><input name="1" type="radio" value="1.1" /><br />
Answer 2: <br />
<input name="a12" type="text" /><input name="1" type="radio" value="1.2" /><br />
Answer 3: <br />
<input name="a13" type="text" /><input name="1"type="radio" value="1.3"  /><br />
Answer 4: <br />
<input name="a14" type="text" /><input name="1" type="radio" value="1.4" />
<br /><br />


<strong>Question 2:</strong> <input name="q2" type="text" /> <br />
Answer 1: <br />
<input name="a21" type="text" /><input name="2" type="radio" value="2.1" /><br />
Answer 2: <br />
<input name="a22" type="text" /><input name="2" type="radio" value="2.2" /><br />
Answer 3: <br />
<input name="a23" type="text" /><input name="2"type="radio" value="2.3"  /><br />
Answer 4: <br />
<input name="a24" type="text" /><input name="2" type="radio" value="2.4" />
<br /><br />

<strong>Question 3:</strong> <input name="q3" type="text" /> <br />
Answer 1: <br />
<input name="a31" type="text" /><input name="3" type="radio" value="3.1" /><br />
Answer 2: <br />
<input name="a32" type="text" /><input name="3" type="radio" value="3.2" /><br />
Answer 3: <br />
<input name="a33" type="text" /><input name="3"type="radio" value="3.3"  /><br />
Answer 4: <br />
<input name="a34" type="text" /><input name="3" type="radio" value="3.4" />


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

FIRST PHP:

<html>
<body>
<form action="actions2.php" method="post">
<strong><?php echo $_POST["q1"];?></strong>
<br /><br />
<?php



echo $_POST["a11"]."<input name='newa1' type='radio' value='1.1' />"."<br />".$_POST["a12"]."<input name='newa1' type='radio' value='1.2' />"."<br />".$_POST["a13"]."<input name='newa1' type='radio' value='1.3' />"."<br />".$_POST["a14"]."<input name='newa1' type='radio' value='1.4' />"."<br />";

echo "<br /><br />"

?>

<strong><?php echo $_POST["q2"];?></strong>
<br><br>
<?php



echo $_POST["a21"]."<input name='newa2' type='radio' value='2.1' />"."<br />".$_POST["a22"]."<input name='newa2' type='radio' value='2.2' />"."<br />".$_POST["a23"]."<input name='newa2' type='radio' value='2.3' />"."<br />".$_POST["a24"]."<input name='newa2' type='radio' value='2.4' />"."<br />";

echo "<br /><br />"

?>

<strong><?php echo $_POST["q3"];?></strong>
<br><br>
<?php



echo $_POST["a31"]."<input name='newa3' type='radio' value='3.1' />"."<br />".$_POST["a32"]."<input name='newa3' type='radio' value='3.2' />"."<br />".$_POST["a33"]."<input name='newa3' type='radio' value='3.3' />"."<br />".$_POST["a34"]."<input name='newa3' type='radio' value='3.4' />"."<br />";

echo "<br /><br />"



?>



<input name="result1" type="hidden" value="<?php echo $_POST['1']; ?>">
<input name="result2" type="hidden" value="<?php echo $_POST['2']; ?>">
<input name="result3" type="hidden" value="<?php echo $_POST['3']; ?>">



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

</form>



<br /><br />
</body>
</html>

SECOND PHP:

<?php




$correctAnswer1 = $_POST['result1'];
$newAnswer1 = $_POST['newa1'];
$correctAnswer2 = $_POST['result2'];
$newAnswer2 = $_POST['newa2'];
$correctAnswer3 = $_POST['result3'];
$newAnswer3 = $_POST['newa3'];
$score = 0;

if($newAnswer1 == $correctAnswer1) {
    $score++;
}
if($newAnswer2 == $correctAnswer2) {
    $score++;
}

if($newAnswer3 == $correctAnswer3) {
    $score++;
}

if ($score >= 2)
{
    echo "Congratulations, you did well. You got ".$score;
}
else if ($score == 1)
{
    echo "Only one, poor show";
}
else 
{
    echo "YOU GOT ZERO YOU SHITE";
}
?>

I'm not totally sure what you wanted, but if you need a way to store the questions and answers you could look into XML or JSON.

PHP should have libraries to encode/decode both of them

I am not sure if I have understood the question pretty well, but this is how you store in a .txt file using php. http://bd1.php.net/file_put_contents also take a look at this http://www.w3schools.com/php/func_filesystem_fwrite.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM