Option selected from one ddl populates another

Hi Experts

I have 3 drop down lists, namely: sectors, sub sectors and categories. Each sector has a number of sub sectors, and each sub sector has a number of categories. Basically what I want to be able to do is allow the user to select a sector from the first ddl, this in turn automatically populates the second ddl (without refreshing the page) with related sub sectors, and once they select the sub sector it automatically populates the third ddl with relevant categories.

I am working with php, but this will probably have to be done with javascript. All the sectors, sub sectors and categories are stored in my DB (There are alot), detailed below.

Sector
id
name

Sub Sector
id
sector_id
name

Category
id
sub_sector_id
name

Any idea on how I would go about this?

Thanks in advance
leapingleonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leapingleonAuthor Commented:
Hi there

Doest help too much. The first link is for ASP.Net and I am using PHP.

The second link links to mainly asp.net links or dead ones. The only decent one I could find involves me having to create the list boxes manually in the javascript. This would be too much, I have to be able to get it from a database. Just so you know, there are 14 sectors, 70 sub sectors and 350 categories.
0
Oddsen84Commented:
There is only one way this can realy happen:

When the user selects one of the first sectors(Populated trough php and MySQL) Javascript must use AJAX to send a request to php retriving the second list based on the users first choise, and the same for for the third based on the second list choise. The lists returned wil trough a "for loop" populate the second and third list.

Im sorry that I can't find any code for proving to you what I say...
0
leapingleonAuthor Commented:
After a bit of research, I eventually decided to implement the javascript approach detailed in one of your links above, the specific one being: http://www.mattkruse.com/javascript/dynamicoptionlist/

I didnt want to type all the 100's of categories out, so I wrote this quick php script, so if anyone in the future reading this is in the same situation as me, just edit this to your specs, and it will make the job alot easier.


<?include('Connect.php');?>
 
<?
 
	$sql  = "SELECT * FROM sectors";
	$result = mysql_query($sql) or die(mysql_error());
	
	while($row = mysql_fetch_array($result))
	{	
		$sectorid = $row['id'];
		$sectorname = $row['name'];
		$text = "SectorSubCategory.forText('" . $sectorname . "').addOptions('";
		$text2 = "";
		$sqlsub  = "SELECT * FROM subsectors WHERE sector_id = " . $sectorid;		
		$resultsub = mysql_query($sqlsub) or die(mysql_error());
		while($newrow = mysql_fetch_array($resultsub))
		{
			$text .= $newrow['name'] . "','";	
			$subsectorid = $newrow['id'];
			
			$sqlcategories  = "SELECT * FROM categories WHERE sub_sector_id = " . $subsectorid;		
			$resultcategories = mysql_query($sqlcategories) or die(mysql_error());
			
			if (mysql_fetch_array($resultcategories))
			{
				$text2 .= "SectorSubCategory.forText('" . $row['name'] . "').forText('" . $newrow['name'] . "').addOptions('";
				while($newrowcategory = mysql_fetch_array($resultcategories))
				{
					$text2 .= $newrowcategory['name'] . "','";		
				}
				$text2 .= "');";
				$text2 .= "<br/>";
			}
			
		}
		$text .= "');";
		$text .= "<br/>";
		echo $text;
		echo $text2;
	}
 
?>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.