Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 =].