Drop Down Menu Redirection
Thursday April 20, 2006
If you go to PBS.org's home page, you will notice a drop down menu called "Explore" that lets you navigate to different areas of the web site. Drop down menu navigation like this is popular because it lets you add a lot of navigation options, without taking up much space. Most sites wouldn't use PHP to do this, but one way to replicate this with PHP is to setup an HTML form somewhere on your page and have it pass the site's URL to a PHP page when you submit it. For example:
Then have the PHP file jump.php use basic redirection to send them to the right page. Something like this:
<?php
$url = $_POST["url"];
header("Location: $url");
?>
<form action="jump.php" method="post">
<select name=url>
<option value="http://php.about.com">About PHP</option>
<option value="http://www.identity.st">Identity</option>
</select>
<input type="submit" value="Go">
</form>
Then have the PHP file jump.php use basic redirection to send them to the right page. Something like this:
<?php
$url = $_POST["url"];
header("Location: $url");
?>


Comments
Sweet stuff, thanks ! i tried to do this myself but i’m not very skilled at php so i got stuck. This works perfectly
This is a bad idea. This script is wide open for abuse, letting 3rd parties use your site to redirect users to anywhere on the internet.
At minimum, you should check the $_SERVER[’HTTP_REFERER’] to make sure the POST is coming from your site, but even that can be spoofed since it is information supplied by the client.
A better way would be to hard-code the redirect URLs in the PHP script itself, and have the form POST tokens to the script. The script would know what the tokens meant, and which redirect URL to use.
I’m sorry you don’t like the idea Dave. This was meant to be a simple solution for people learning PHP. While more security could be built into it if the programmer felt the need, I think this conveys the basic idea of how to do the menu. Like all of our learning scripts, we encourage people to build upon this base to fit their needs and to improve them as they learn more PHP.
This allows the link to open up in the same window. What would I need to do to allow it to work in a new window?
How would i do it without a drop down menu but rather an input type text field where the user inputs a certain value and it redirects to a certain page on the site. And if they type something different it goes to a different part on the site?
Thanks for this information. I searched for this code all over the net. It was too complicated for me to understand. Then I found you! THANKS!!
you can send a variable back to the same page and write an if statement that included the file or redirect based on the variable. Is this possible?
Hello, your tutorial is too simple and very interesting. I found it usefull and easiest way on how to use a DropDown list. Thanks you, and I am sorry for my poor english.