1. Home
  2. Computing & Technology
  3. PHP / MySQL
photo of 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.

Leave a Comment

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

Explore PHP / MySQL

More from About.com

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

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

All rights reserved.