Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Milton Centeno
PLUS
Milton Centeno
Courses Plus Student 7,940 Points

PHP header redirect method does not work

Here are two basic php files. page1.php should redirect to page2.php when the submit button is pressed but it does not. Why? Thanks.

<?php if($_SERVER["REQUEST_METHOD"] == $_POST) { header("Location: page2.php");
exit; } ?>

<html>

<form action="page1.php" action="post">
    <input type="submit" value="go to page two">
</form>

</html>

5 Answers

one small problem, <form action="page1.php" action="post"> you need to change the action="post" to method="post"

If you want you can check our previous chat and you will see the answer there but i will say it again here, the $_server function needs to look like this - $_SERVER['REQUEST_METHOD'] == 'POST', and not $_POST.

Milton Centeno
PLUS
Milton Centeno
Courses Plus Student 7,940 Points

Yes. I tried that as well. But it did not work. Here is what the code looks like now edited.

<?php if($_SERVER['REQUEST_METHOD'] == 'POST') { header("Location: page2.php");
exit; } ?>

<html>

<form action="page1.php" action="post">
    <input type="submit" value="go to page two">
</form>

</html>

Milton Centeno
PLUS
Milton Centeno
Courses Plus Student 7,940 Points

That did it. Wow what a silly thing to overlook. Thank you very much. Funny thing is I've posted this same question elsewhere with many responses and you are the first to notice.

Glad i could help out =].