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
C_Calav
Forum Contributor
Posts: 395 Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand
Post
by C_Calav » Sun Jan 30, 2005 5:04 pm
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!
magicrobotmonkey
Forum Regular
Posts: 888 Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA
Post
by magicrobotmonkey » Sun Jan 30, 2005 5:33 pm
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>
C_Calav
Forum Contributor
Posts: 395 Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand
Post
by C_Calav » Sun Jan 30, 2005 5:36 pm
thank you very much magicrobotmonkey!
C_Calav
Forum Contributor
Posts: 395 Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand
Post
by C_Calav » Sun Jan 30, 2005 5:50 pm
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>
C_Calav
Forum Contributor
Posts: 395 Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand
Post
by C_Calav » Mon Jan 31, 2005 1:35 am
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
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Mon Jan 31, 2005 3:27 am
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.