• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

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
0
leapingleon
Asked:
leapingleon
  • 2
  • 2
1 Solution
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now