Question about drop down menus

Hi, I’m mapping out a site at the moment and I would like to add a feature but I’m not sure how complex it would be to implement it etc.

Basically i’d like to be able to have a drop down menu with categories, for an example of one “Family” then when Family is chosen from the 1st drop down menu, next to it a second drop down populates with options related to the first. ie. “Family” “Parenting” or “Relationships”.

How difficult would something like this be to implement? and is it possible in php? possible with if statements?

Well, there are many many ways of handling this type of function. It can be handled in Javascript or Jquery or even PHP.

In PHP, you would have to do one of several ways possible. First, you can track the current values of a dropdown by saving it inside of SESSION variables and just refresh the page. In this manner, you would check the SESSION variable to see if the first dropdown was selected. If so, you display it with the current value selected and then go to the next dropdown and load whatever is needed inside that one, etc.

There are advantages to this way which is manly security. No outside code show to the user. (Anyone can RIGHT-CLICK on ANY page on ANY site and do a VIEW-SOURCE!) If you use PHP, they can not view your source. But, with Javascript/Jquery, they can see that code because it is CLIENT-SIDE code. (PHP is SERVER-SIDE code and does not exist once inside the browser!)

So, to handle it is PHP, you just create SESSION variables for the first dropdowns. Set up your OPTIONS in your dropdown select tag’s to include a lowest level setting. This means to use the first dropdown option to be something like value=0, displaying “Select your option”. When the page is posted back to itself, it checks for this dropdown’s value and if it is 0, it sets it to that value and all the other dropdown’s that follow. If they select the next value for the first dropdown, the PHP code sets their value as the SELECTED version and then sets the next dropdown to the correct input options.

Hopefully, this helps explain and give you a starting point. If you can not figure it out, ask further questions. I am sure we can help with this one. Good luck!

I use this system within sites I admin, i’ll post the code below, first I’ll explain how it works.

I use it for timezones. I have written a class that returns a multi dimension array called $timezones which is populated like so: (note: I have chopped this class down just enough so you can see the structure)

[php]class Timezones {
public function africa() {
$africa_timezones = array(‘Africa/Abidjan’, ‘Africa/Accra’, ‘Africa/Addis_Ababa’);
return $africa_timezones;
} // end function.

public function all() {
	$timezones['Africa']    = $this->africa();
	$timezones['America']   = $this->america();
	//etc. etc.
	return $timezones;
} // end function.

}

$Timezones = new Timezones;
$tz_all = $Timezones->all();
[/php]

So before using this next part, you will need to gather your data into an array of arrays of the info you wish to present.

Now, the fun bit…

HTML:

[code]

Please Choose<?php
foreach($tz_all as $x => $y) {
print ‘’ . $x . ‘’;
} ?>

[/code]

Javascript:

[code][/code]

You will need to make a couple of tweaks to fit your needs, but it will do what you require.

Cheers,
Red. :wink:

Thank you both very much for the helpful responses, I’ll read through and have a play and see how I go :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service