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,791
  • 10
  • 36
  • 49
  • 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
  • 10,042
  • 11
  • 48
  • 64
  • hello, I tried this code but it's still going to the "response.html" even when the answer is correct – user2953989 Jan 30 '14 at 22:49
  • Try the lower block of code; it shouldn't redirect to response.html. Which brings me to my next question...are you configured to interpret *.html files as PHP files? – Chris Forrence Jan 30 '14 at 22:52
  • I also tried that but any answer redirects to a page with just the form and no headers. I'm really at a loss with this one! – user2953989 Jan 30 '14 at 22:52
  • Sorry, I'm quite new to this and I'm not really sure what you mean – user2953989 Jan 30 '14 at 22:53
  • In addition, @user3005003, I did modify my answer just a few minutes ago. The hidden inputs that I had been echoing were outside of their form, so that would make them not interpreted. – Chris Forrence Jan 30 '14 at 22:53
  • @user3005003, I got it. The last hidden input with name="answer" was messing with the form submission. I have it correctly redirecting on my local system. – Chris Forrence Jan 30 '14 at 22:56
  • The second solution just makes the first page reload, and unfortunately the first solution still sends me to response.html! – user2953989 Jan 30 '14 at 23:00
  • You're a genius, I can't thank you enough :) – user2953989 Jan 30 '14 at 23:05