我遇到了麻烦,我无法理解为什么。
这很简单。从文件中的随机行读取,向用户显示问题并接收答案。如果答案正确,请检入其他文件。
//open the file and count the number of lines (i.e; the number of questions)
$pointer = fopen('questions.txt','r');
$num=0;
while(!feof($pointer)){
$line = fgets($pointer);
$num++;
}
//take a random question from the file
$rnd = mt_rand(0,$num-1);
$question = file('questions.txt');
$mess = '';
if(isset($_POST['text'])){
//take the number of the line and check if both answers are the same
$rnd2 = $_POST['val'];
$answer_usr = $_POST['text'];
$answr_file = file('answers.txt');
$answ = $answr_file[$rnd2];
if($answ == $answer_usr)
{$mess = 'Correct';}
else
{$mess = 'Try again';}
}
HTML代码如下:
<form method="POST" action="">
<span><?php echo $question[$rnd] ?></span>
<input type="hidden" name="val" value="<?php echo $rnd ?>"/>
<div>
<input type="text" name="text"/>
<input type="submit" value="click"/>
</div>
<span><?php echo $mess ?></span>
</form>
我已经检查过两个答案都是一样的,但$ mess变量总是说&#34;再试一次&#34;。
有人知道我的错误在哪里吗?
非常感谢。