Gldnbr Posted February 17, 2011 Share Posted February 17, 2011 Hello PHP Freaks forum, I am a beginner in web development and seem to be understanding it very quickly, but there is one thing that is putting this into some halt. I have this code right here, what I want to do is have a "riddle site", where if you answer the riddle correctly, it redirects you to another page (in other words to the next riddle). Object: If correct, give access to next page. If incorrect, stay on page and print "Incorrect". I've tried to start on it, but the way how HTML and PHP don't mix very well, I feel like in the middle of an ocean feeling helpless. <html> <head> <title>PHP Test</title> </head> <body> <p> <font face="Courier New">ZnJ5cyBwYmFnbnZhcnEgaGFxcmVqbmdyZSBvZXJuZ3V2YXQgbmNjbmVuZ2hm</font> </p> CLUE: The base is rotated <br/></body> <form action="index-5.php" method="post"> Answer: <input type="text" name="number" /><br /> </select> <input name="submit" type="submit"> </form> <?php if(isset($_POST['submit'])){ $number = $_POST['number']; if ($number == "scuba"){ echo "CORRECT";} } ?> </html> If possible, can you give me an explanation how to do it and a sample of it as well? Thanks, Gldnbr Quote Link to comment Share on other sites More sharing options...
petroz Posted February 17, 2011 Share Posted February 17, 2011 This is untested, but header is the method for redirect. if ($number == "scuba") { //echo "CORRECT"; header('Location: success.php'); } Quote Link to comment Share on other sites More sharing options...
lalnfl Posted February 17, 2011 Share Posted February 17, 2011 Make sure it is formatted like this: header("Location: http://www.mysite.com/success.php"); exit(); Make sure you include the exit() function Quote Link to comment Share on other sites More sharing options...
Gldnbr Posted February 17, 2011 Author Share Posted February 17, 2011 Oh okay, I see now, I didn't know you can do that. But now what do you do with this? <form action="index-5.php" method="post"> I need to remove that so it doesn't redirect to index-5.php when I click submit, instead it would redirect to the URL if correct. I can't just remove it like this, can't I? <form action="" method="post"> Quote Link to comment Share on other sites More sharing options...
petroz Posted February 17, 2011 Share Posted February 17, 2011 <form action="index-5.php" method="post"> does not redirect to the index-5.php, it simply tells the form where to submit. So if your form is on the same page as your PHP script that processes it then you can use <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> Quote Link to comment Share on other sites More sharing options...
Gldnbr Posted February 17, 2011 Author Share Posted February 17, 2011 I am honestly confusing myself more than I should. Why am I feeling stressed out! The error is telling me that a header modification is already in use, I THINK. Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\index-4.php:12) in C:\xampp\htdocs\index-4.php on line 23 <html> <head> <title>PHP Test</title> </head> <body> <p> <font face="Courier New">ZnJ5cyBwYmFnbnZhcnEgaGFxcmVqbmdyZSBvZXJuZ3V2YXQgbmNjbmVuZ2hm</font> </p> CLUE: The base is rotated <br/></body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> Answer: <input type="text" name="number" /><br /> </select> <input name="submit" type="submit"> </form> <?php if(isset($_POST['submit'])){ $number = $_POST['number']; if ($number == "scuba"){ header("Location: http://localhost/index-3.php"); exit();} } ?> </html> Quote Link to comment Share on other sites More sharing options...
trq Posted February 17, 2011 Share Posted February 17, 2011 You cannot call header() after output has been sent to the browser. You will need to put your processing code bofore the html. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted February 17, 2011 Share Posted February 17, 2011 You can't have any output written to the browser if you want to use a HTTP header command. Not even a space or new line. PHP apps tend to be written so all PHP processing is done before output is written. That way, you can determine if you need to redirect, or display something, and do either without interruption. Its a good habit to get into. Quote Link to comment Share on other sites More sharing options...
Gldnbr Posted February 17, 2011 Author Share Posted February 17, 2011 Okay thanks, solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.