Page 1 of 1
Dynamic select box
Posted: Sun Jan 30, 2005 5:04 pm
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ї's_id'];
$query = "select * from tbl_emp";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
$e_dept = $rowї'e_dept'];
?>
<option value="1st Flight"> <?php echo $e_dept; ?> </option>
<?php
}
?>
</select>
thanx for your help again guys!
Posted: Sun Jan 30, 2005 5:33 pm
by magicrobotmonkey
Code: Select all
<select name="select_dept">
<option value="0" selected>Select Dept</option>
<?php
include("conection.php");
$s_id = $_GETї'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++)
{
$row = mysql_fetch_array($result);
$e_dept = $rowї'e_dept'];
?>
<option value="1st Flight"> <?php echo $e_dept; ?> </option>
<?php
}
?>
</select>
Posted: Sun Jan 30, 2005 5:36 pm
by C_Calav
thank you very much magicrobotmonkey!
Posted: Sun Jan 30, 2005 5:50 pm
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)
{
var select2 = document.form1.select2;
select2.options.length = 0;
if (o == "1")
{
select2.optionsїselect2.options.length] = new Option('Apple');
select2.optionsїselect2.options.length] = new Option('Pear');
}
if (o == "2")
{
select2.optionsїselect2.options.length] = new Option('Carrot');
select2.optionsїselect2.options.length] = new Option('Potatoe');
}
if (o == "3")
{
select2.optionsїselect2.options.length] = new Option('Chicken');
select2.optionsїselect2.options.length] = new Option('Fish');
}
}
</script>
</head>
<body>
<form name="form1">
<select name="select1" size="1" onchange="setOptions(document.form1.select1.optionsї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>
Posted: Mon Jan 31, 2005 1:35 am
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
Posted: Mon Jan 31, 2005 3:27 am
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.