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");
?>


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.
sir/madam who is suppose to answer php problem plz give me u id i need help for php as my skills are very week but i am working on project plz do help me out
thanks
To open it in a new window, just add the attribute for the form element, target=”_blank”
it looks like this
php site
site
sorry this webpage is not allowing me to enter the complete html code for the form.
so
the code after adding it to form is like this
Very simple idea, will try it out and report back on what happens.
This was such a great help! I was trying to do something else (make a contact form that takes you to a thank you page at the end) but because you boiled it down so simply I was able to figure it out. Thank you SO MUCH!
Sorry – the stupid flashing ad telling me I’d won something just put me off.
Didn’t read any further…
For the page itself redirecting you to another page, you can put something like this. I’m writing the algorithm, and not the code itself since I’m guessing the web application wouldn’t allow me to. So here it is:
Before the HTML tag, put this:
if $_post['url'] exists //can do this with the isset() function.
header(…);
Then open the HTML tag and so on.
Good luck!
P.S. I have just started learning PHP for two days, so correct me if I’m wrong.
This is a great approach. I have a pulldown list where I want to select and pass a record ID to a specific URL, which uses that ID to SELECT data for its own form. Since this (target) file’s form’s action is $_SERVER["PHP_SELF"], I couldn’t use the (target) filename as the ACTION in my pulldown file (wrong $_POST variables). So I’m taking your generalized jump file and concatenating the target file’s URL with an integer value from the pull down list e.g.: “myfile.php?var=N” and passing my record ID for the SELECT in that way. I am still interested in some more elaboration on security implications and alternative ways to go from pulldown lists to other files ! THanx !