1

Here is a simple maths quiz i've programmed but it's not displaying correctly. I want it to ask the question, then if the user responds correctly go to one webpage, if the respond incorrectly go to another. And if they enter a non numerical value to go to another webpage.

I got it to ask the question but I still haven't worked out how to display the correct webpage, but now nothing works :(

My browser says there's a problem with the line of my first if statement, but I can't see one:

<?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>
user2953989
  • 2,431
  • 9
  • 31
  • 47
  • Are you missing an opening – ashatch Jan 30 '14 at 22:25
  • No, sorry, I didn't indent that properly, it's there! – user2953989 Jan 30 '14 at 22:27
  • According to [this](http://php.net/manual/en/function.isset.php#refsect1-function.isset-returnvalues), `isset` returns either TRUE or FALSE. I would use an if-else check for seeing whether or not `$_POST['answer']` is set. – Chris Forrence Jan 30 '14 at 22:31
  • so i should do if(isset($_POST['answer'])) {} else {} ? – user2953989 Jan 30 '14 at 22:33
  • That would be a lot clearer, yes! However, there appears to be a very serious issue; how do you know `$first` and `$second` are the same number when you refresh the page? Since they'd likely be different, a correct answer could be evaluated as incorrect! – Chris Forrence Jan 30 '14 at 22:36
  • i've edited the code to what i have now. the first page works fine but all answers are directing to the "response.html". So you're right I need to change this but i'm not sure how! – user2953989 Jan 30 '14 at 22:37
  • any ideas? I feel like i've tried everything! – user2953989 Jan 30 '14 at 22:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46470/discussion-between-chrisforrence-and-user3005003) – Chris Forrence Jan 30 '14 at 23:02

2 Answers2

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

change to

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

Missing second close bracket.

Ben
  • 11
  • 1
  • I understand what you're doing (fixing the syntax error), but that would evaluate to `is_int(TRUE)` or `is_int(FALSE)`, which in either case, is false. [Reference](http://www.php.net/manual/en/function.is-int.php#refsect1-function.is-int-returnvalues) – Chris Forrence Jan 30 '14 at 22:34
1

You need to send both $first and $second along with your post, then explicitly cast them and $_POST['answer'] to integers (after verifying that they are, at the very least, numeric).

In addition, you can remove the else clauses to make sure response.html doesn't get fired. You can get away with this because you already call exit() after you send the Location header.

<?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>
Chris Forrence
  • 9,860
  • 11
  • 47
  • 62