PHP数学测验程序无法正确显示

时间:2014-01-30 22:22:23

标签: php html forms math

这是一个简单的数学测验我已经编程但它没有正确显示。我希望它提出问题,然后如果用户正确回答,请转到一个网页,如果回复不正确则转到另一个网页。如果他们输入一个非数字值去另一个网页。

我得到了问题,但我还没有找到如何显示正确的网页,但现在没有任何作用:(

我的浏览器说我的第一个if语句的行有问题,但我看不到一个:

<?php
$first = Rand(1,10);
$second = Rand(1,10);

if(isset($_POST['answer'])){
     if(is_int($_POST['answer'])) {

        if($first * $second == $_POST['answer']) {
        header("Location: correct.html");

        exit();  
        }
        else {
        header("Location: incorrect.html");

        exit();     
        }   
    }
    else {
        header("Location: response.html");

        exit(); 
    }   

}
else{
    echo "<h1>What is " . $first . " times " . $second . "?" . "</h1>";

}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Maths Quiz</title>
</head>

<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>Answer<br/>
<input type="text" id="answer" name="answer" /></p>
<p></p>
<button type="submit" name="submit" value="send">Submit</button>
<input type="hidden" name="answer" value="<?php echo $answer; ?>"/></p>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

  if(is_int($_POST['answer'] == 1) {

更改为

  if(is_int($_POST['answer'] == 1)) {

缺少第二个关闭括号。

答案 1 :(得分:1)

您需要同时发送$first$second以及帖子,然后明确地将它们和$_POST['answer']转换为整数(在验证它们之后,至少是数字)

此外,您可以删除else子句以确保不会触发response.html。您可以放弃此操作,因为您在发送位置标题后已经致电exit()

<?php
$m = '';
if (isset($_POST['answer'])) {
    if(is_numeric($_POST['answer'])
      && is_numeric($_POST['first'])
      && is_numeric($_POST['second'])) {
        $first = intval($_POST['first']);
        $second = intval($_POST['second']);
        $answer = intval($_POST['answer']);

        if ($first * $second == $answer) {
            header("Location: correct.html");
            exit();
        } else {
            header("Location: incorrect.html");
            exit();
        }
    }
}
$first = Rand(1, 10);
$second = Rand(1, 10);

$m = "<input type='hidden' name='first' value='" . $first . "' />"
    . "<input type='hidden' name='second' value='" . $second . "' />"
    . "<h1>What is " . $first . " times " . $second . "?" . "</h1>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Maths Quiz</title>
    </head>

    <body>
        <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <?php if(strlen($m) > 0) { echo $m; } ?>
            <p>Answer<br/>
                <input type="text" id="answer" name="answer" /></p>
            <p></p>
            <button type="submit" name="submit" value="send">Submit</button>
        </form>
    </body>
</html>