Dynamic select box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Dynamic select box

Post by C_Calav »

hi guys, just investigating on how to do dynamic select boxes read the tutorial and some other sites.

this is what i have (so far)

is this the best way of doing this? and if i have two or more e_dept the same how do i only display one of them in my select box.

at the moment for example the options look like this

IT
Payroll
IT
Admin
IT
Payroll

Code: Select all

<select name="select_dept">
<option value="0" selected>Select Dept</option>

<?php
	include("conection.php"); 	
	$s_id = $_GET&#1111;'s_id'];

	$query = "select * from tbl_emp";
  	$result = mysql_query($query);
  	$num_results = mysql_num_rows($result);
	
	for ($i=0; $i <$num_results; $i++)
  	&#123;
	$row = mysql_fetch_array($result);
     
     	$e_dept = $row&#1111;'e_dept'];
?>

<option value="1st Flight"> <?php echo $e_dept; ?> </option>

<?php
&#125;
?>

</select>
thanx for your help again guys!
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Code: Select all

<select name="select_dept">
<option value="0" selected>Select Dept</option>

<?php
   include("conection.php");    
   $s_id = $_GET&#1111;'s_id'];

   $query = "select DISTINCT(e_dept) from tbl_emp";
     $result = mysql_query($query);
     $num_results = mysql_num_rows($result);
   
   for ($i=0; $i <$num_results; $i++)
     &#123;
   $row = mysql_fetch_array($result);
     
        $e_dept = $row&#1111;'e_dept'];
?>

<option value="1st Flight"> <?php echo $e_dept; ?> </option>

<?php
&#125;
?>

</select>
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thank you very much magicrobotmonkey!
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

ok, now on to the next part..

i have found this JavaScript which seems to work well... looking at it twice now i havent seen any other Javascript so i dont know if it is suteable for my php script or not.

but how do i dynamicly fill the contents of the second box based on my first choice? would i put PHP variables in the javascript?

the two select boxes contents will allways be changing??

thanx again for the help!

Code: Select all

<html> 
<head> 
<script language="javascript"> 

function setOptions(o) 
&#123; 
var select2 = document.form1.select2; 
select2.options.length = 0; 
if (o == "1") 
&#123; 
select2.options&#1111;select2.options.length] = new Option('Apple'); 
select2.options&#1111;select2.options.length] = new Option('Pear'); 
&#125; 
if (o == "2") 
&#123; 
select2.options&#1111;select2.options.length] = new Option('Carrot'); 
select2.options&#1111;select2.options.length] = new Option('Potatoe'); 
&#125; 
if (o == "3") 
&#123; 
select2.options&#1111;select2.options.length] = new Option('Chicken'); 
select2.options&#1111;select2.options.length] = new Option('Fish'); 
&#125; 
&#125; 
</script> 
</head> 
<body> 
<form name="form1"> 
<select name="select1" size="1" onchange="setOptions(document.form1.select1.options&#1111;document.form1.select1.selectedIndex].value);"> 
<option value="1">Fruit</option> 
<option value="2">Vegetable</option> 
<option value="3">Meat</option> 
</select> 
<br /> 
<br /> 
<select name="select2" size="1"> 
<option>Apple</option> 
<option>Pear</option> 
</select> 
</form> 
</body> 
</html>
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

should i rephrase my question or can some one point me in the right direction?

i can find JScript on how to populate a select/list box from a previous option but how do i do it all dynamicly so i dont have to pre type in my options in the javascript?

do i do it the same way but just insert the php variables in place?

thanx
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

viewtopic.php?t=29084 has an example of some Javascript for dynamic select boxes.

In general you need to clear the 2nd select list and rebuild it. This method is up to you but the tutorial stores information in arrays build using php with the information extracted from a database.
Post Reply