我有一个可自定义的测验网站,用户可以在其中输入问题和答案。我的第一个PHP文件获取输入的信息并生成测验页面。答案在隐藏文本框中。然后,第二个PHP将输入的单选按钮与隐藏文本框进行比较,以查看用户是否正确并给出与之相关的分数。我想将问题和答案存储在.txt文件中,然后其他用户可以访问该文件,遗憾的是,作为项目的一部分,我们无法使用MySQL。希望我很清楚,我试图采用这么多方法,但都破坏了我的代码。任何想法都会非常感激。
我的代码如下:
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>
第一个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>
第二个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";
}
?>
答案 0 :(得分:0)
我不完全确定你想要什么,但如果你需要一种存储问题和答案的方法,你可以查看XML或JSON。
PHP应该有库来编码/解码它们
答案 1 :(得分:0)
我不确定我是否已经很好地理解了这个问题,但这是你使用php存储在.txt文件中的方式。 http://bd1.php.net/file_put_contents也会看一下http://www.w3schools.com/php/func_filesystem_fwrite.asp