1. Home
  2. Computing & Technology
  3. PHP / MySQL
Angela Bradley
Angela's PHP / MySQL Blog

By Angela Bradley, About.com Guide to PHP / MySQL

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:

<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
April 21, 2006 at 8:39 am
(1) G says:

Sweet stuff, thanks ! i tried to do this myself but i’m not very skilled at php so i got stuck. This works perfectly :)

May 12, 2006 at 1:03 pm
(2) Dave says:

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.

May 12, 2006 at 2:40 pm
(3) php says:

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.

May 31, 2006 at 4:59 pm
(4) Brenty says:

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?

May 18, 2007 at 5:23 pm
(5) Mike says:

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?

June 15, 2007 at 9:54 pm
(6) Regina says:

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

October 15, 2007 at 3:33 pm
(7) michael says:

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?

November 20, 2007 at 10:04 am
(8) Rosário says:

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.

December 10, 2008 at 5:54 am
(9) damanpreet says:

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

December 17, 2008 at 8:55 am
(10) fraze says:

thanks

January 8, 2009 at 11:47 pm
(11) rrr says:

To open it in a new window, just add the attribute for the form element, target=”_blank”

it looks like this

php site
site

January 8, 2009 at 11:49 pm
(12) rrr says:

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

January 28, 2009 at 12:29 pm
(13) Harpsih says:

Very simple idea, will try it out and report back on what happens.

February 18, 2009 at 3:29 am
(14) Becky says:

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! :)

April 4, 2009 at 8:05 pm
(15) Rob Grattan says:

Sorry – the stupid flashing ad telling me I’d won something just put me off.

Didn’t read any further…

May 6, 2009 at 10:17 am
(16) Parham Doustdar says:

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.

May 22, 2009 at 11:51 am
(17) FeralReason says:

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 !

November 23, 2009 at 10:38 am
(18) FIlipe says:

Reply @ (2) comment:
This is not unsecure. The “abuse” you refer too is just people using the redirect for their own websites, which poses no threat on website security, just possible abuse of bandwith, nothing else. To prevent it you can just include a check for “http://” on $_POST['URL'], avoiding redirects to external websites, which would null the point of using it in 3rd party sites.
Once again, it still poses no security threat as most of the simpler solutions never do.
The simpler the code, the safer it is.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore PHP / MySQL
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL

©2009 About.com, a part of The New York Times Company.

All rights reserved.