0

I'm trying to create a maths quiz page. The first page needs to generate a question shown as a header, that asks the user what two random numbers are multiplied together. Then depending on the users input, it takes them to a different page. If they are correct it displays the paragraph "You are correct!". If they are wrong it displays the paragraph "You are incorrect" and invites the user to try again. and if they enter a string it displays the paragraph "I don't understand your response" and invites the user to try again.

So far I have the below code, the layout is correct but the header isn't working, and I've attempted to display a new page, but again, they don't load. Anyone know where I'm going wrong?

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

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

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

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

        exit();     
    }   
}
else if(is_string($_POST['answer']) == 1) {
    header("Location: response.html");

        exit();     
}

 ?>
 <!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['file:///X|/Software Development/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
  • 1
    It's because you're sending data to the header before you perform the location redirect. (hint, move your `echo`) – Ohgodwhy Jan 30 '14 at 21:13
  • don't you see a warning? If not, enable them. – Karoly Horvath Jan 30 '14 at 21:16
  • @Ohgodwhy ack, didn't see your comment. – mellowfish Jan 30 '14 at 21:17
  • give a title which describes the *problem*, not the a description of your program (it's really irrelevant...) – Karoly Horvath Jan 30 '14 at 21:17
  • There are also logical problems with your code: you have two inputs with the name "answer"; you are using $_POST['first'] and $_POST['second'] but you never actually post it. – Biri Jan 30 '14 at 21:19
  • It's saying I have an unidentified index? thank you for your help so far – user2953989 Jan 30 '14 at 21:24
  • I used answer in the hidden input as well to store the original value input by the user into the form. Is this not correct? does it need a different name? – user2953989 Jan 30 '14 at 21:31
  • possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) –  Jan 30 '14 at 21:53

2 Answers2

2

You must have the header() code before any output, especially that echo statement.

mellowfish
  • 664
  • 6
  • 14
1

Apart from sending the headers before you output something, i think your form action is wrong, it should be

action="<?php echo $_SERVER['PHP_SELF']; ?>"

You are sending the POST via FILE protocol, it won't be processed by Web Server/PHP.

And also, you are not POSTing "first" and "second" fields.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
John Lucabech
  • 156
  • 1
  • 7