Jump to content

Answer correct; redirect to another page?


Gldnbr

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/227943-answer-correct-redirect-to-another-page/
Share on other sites

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">

<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">

 

 

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>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.