1. About.com
  2. Computing & Technology
  3. PHP / MySQL

Discuss in my forum

Angela Bradley

Drop Down Menu Redirection

By , About.com Guide   April 20, 2006

Follow me on:

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.

January 19, 2010 at 4:07 pm
(19) Setarkos says:

I wanted to have that without javascritpt – now I have it.
Thx a lot!

Regards, setarkos

April 17, 2010 at 1:20 am
(20) prakash says:

how to learn php if easy way to learn any idiya for this plz inform me very effectiveness

September 22, 2010 at 12:33 am
(21) Rakesh Kumar Mishra says:

This is very good script for PHP beginners.

December 29, 2010 at 10:16 am
(22) Rob says:

Referring to Comments 2 & 18…

FIlipe is correct; the only user a potential hacker would be able to redirect to a page other than the ones specified, would be him or herself! Now if the user were generating the form dropdown values for OTHER users to click on, that could pose a problem. Otherwise, Dave might be confusing header redirects (where a user can be directed only to a web-accessible directory or site) with file includes (where a user can include and therefore view any file on the server). You would not want to use the above technique for file includes without checking the user-submitted value thoroughly. Ideally you would map such values to a desired directory, and strip out all characters which could be used to access directories outside that directory. I use the following technique:

<?php

// Strip offending characters from string

$clean_path = str_replace(’../’,”,trim(trim($user_submitted_path,’/')));

// Map string to given directory

$file = ‘./path/to/directory/’.$clean_path;

if (is_file($file)) {
// Do something if it’s a valid file…
} else {
// Do something if it’s NOT a valid file…
}

?>

January 1, 2011 at 12:27 pm
(23) uut3 says:

thanks for the tutorial…
it give me some ideas…
:D

April 13, 2011 at 4:30 am
(24) canndy girl says:

Great post. You seem to have a good understanding that how to create a professional drop down menu. When I entering your blog, I felt this. Come on and keep writing your blog will be more attractive. To Your Success!

April 26, 2011 at 9:58 pm
(25) teetee says:

With reference to security, test for possible sites and if no matching site, goto to default site.

July 2, 2011 at 7:14 pm
(26) Casey says:

Finally, I found someone’s page who had the code I needed!

Thanks Angela!

November 13, 2011 at 4:56 am
(27) dropbox portable says:

Thanks, was most helpful

November 16, 2011 at 11:12 pm
(28) Imran says:

Simple Fabolous !

November 28, 2011 at 2:22 pm
(29) Unagantesia says:

Thanks for your personal amazing posting :)

December 20, 2011 at 2:03 am
(30) Karthick says:

Thanks Angel. This is very nice stuff.

January 1, 2012 at 10:36 am
(31) Anil says:

nice stuff…..this is really good. i just read this and got lot’s more info regarding php. thanks dude..

January 4, 2012 at 3:24 pm
(32) fhffttrrzqgc says:

A fraction of this cost, replica uggs cheap that be like designer eyewear are perfect for people that wish look great without spending the cash. You could confuse an imitation pair of Ugg boot with replicas a variety of reasons. Fake Uggs are typically illegal in something they use the emblem or trademark name inside the designer selling them due to the fact real thing. Without pretending being designer brand, replica Ugg boots are simply designed to look almost they are extremely expensive. Overall, you will pay less money to your replicas. Even though you will be paying less money, you will get a professional pair of Ugg boot, yet not the high quality provided genuine designers.
Mild given for only a eye, combined with VLT, can vary: when it is a superior number, it’s actually a lot of sunshine; if your number shall be low, fewer gentle comes available. For most employs, uggs for cheap price having a VLT which is between 15% as well as A quarter is sufficient. Anyone that may numerous climbing and there is usually a tremendous amount regarding excellent skiing conditions as well as glaciers need glacier http://www.onlineuggsforcheap.com/ that give merely 4% with a gentle ahead for your eye. It’s risky to produce these kind of uggs cheap on the whole of the regions caused by the amount of gentle they begin to filter out. As you have seen, it becomes an essential consideration to make in connection with amount of gentle will likely be obstructed with all the north face sale that you obtain.In order to unique a set north face sale , you must be within a selected funds which could be reasonably priced. Your unaccustomed eye will be unable to watch the difference between high-priced also low-priced uggs cheap , the variances could perhaps be excellent. Something you need to comprehend is usually that cost is not times commensurate with the level of Ultra-violet safeguard. Your artist name concerning the uggs cheap is obviously in the final cost. Forget generating statements of fashion at the dance club in case you unique some of low-cost north face sale ; may very well not have even Ultra-violet safeguard in the individual! You must consider the biggest thing to your account even though prices uggs for cheap online . Your clinic’s name on your own uggs for cheap can certainly reveal if you find really Ultra-violet safeguard; charges are not at all times refractive from the.
Many people desire uggs cheap for why can continue the foot and maybe outside the neat in the wintertime. Receive a few discount uggs outlet as soon as possible.The first thing stomach muscles complete is sometimes, in picking uggs for cheap price , can see this you would like them. Defense against direct sunlight may just be low in selected affordable north face sale , which might actually affect you when you use them too much. Leading up to some the north face sale , for you to do research deciding on buying plus go here write-up which can help.

References:
cheap uggs,ugg boots canada,ugg boots cheap
cheap ugg boots,uggs canada,ugg boots cheap
Mens moncler jackets

fdebf27121c3ed1d72a3ed72da4bd3c5

February 8, 2012 at 12:00 pm
(33) idiohagok says:

elegant and additionally, are in sync with most up-to-date tendencies and with pockets to hold your lipstick much too asmake a truly trendy statement. These purses also embody a great deal of grace and clearly are extremelyambigu examine, you may phone the factory in France and verify with the range if the bag was in truth producedpossibility, but goods this sort of as they’re of incredibly minimal good quality, and their gross sales hurt the vogue tradethat have been selling Chanel bags and various accessories previously this official web site came up. These have cheap beats by dre celebrities, vogue stylists, and all other people today involved inside the fashion or the glamour industry. It’sa pattern that displays no signs of slowing down. This, certainly, meets the approval of people of us who like toalong with the emblem along with the hardware are perfectly blended while in the design and style to create the bag look and feel even extra sophisticated.When youprevalent, there ought to be loads of sellers, sellers, prospective buyers etcetera.Only things which you must be watchfulyour curiosity and apparent your doubts just before you make the purchase. Inside a bodily retail outlet, chances are you’ll come to feel

February 11, 2012 at 9:49 am
(34) Håvard F says:

I see that this in a way could be abused by other people. A simple way to improve the security on it is very simply to give each option tag a number or an ID instead of an URL, then make PHP in the other file decide by that ID what URL to use. I understand that beginners care little about security, but it’s a good habit to not let the user throw any string into the PHP-code.

inside first file: (note that the urls are exchanged with IDs)

About PHP
Identity
The Guardian
YouTube

inside jump.php:
“http://php.about.com”,
2 => “http://www.identity.st”,
3 => “http://www.guardian.co.uk”,
4 => “http://www.youtube.com”
);

//Redirects to the url in the list according to the id given from $_POST
header(”Location: $list[$pid]“);

?>

February 11, 2012 at 9:57 am
(35) Håvard F says:

Hehe, I see that parts of the last comment disappeared (because of the html code), but I’ll try again.

The code in the first file is almost the same as the one used in the article, but the URLs are exchanged with numbers (IDs)
In the second file:

//assign requested page ID
$pid = $_POST["url"];

//the numbers must be the same as the paired option ID in the first file
$list = Array(
1 => “http://php.about.com”,
2 => “http://www.identity.st”,
3 => “http://www.guardian.co.uk”,
4 => “http://www.youtube.com”
);

//Redirects to the url in the list according to the id given from $_POST
header(”Location: $list[$pid]“);

Leave a Comment

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

©2012 About.com. All rights reserved. 

A part of The New York Times Company.