dropdown list in php
hi guys, i have a question. i wanna have a webpage coded in php, with several dropdown lists. i would like to know if i can have the 2nd list populated with choices according to 1st list? that is , if i choose "one" in the 1st dropdown menu, the 2nd dropdown menu will show "oneA", "oneB", "oneC" . if i choose "two" , 2nd menu will show "twoA" ,"twoB" and so on....
please advance.
Thanks
Hobee
[417 byte] By [
hobee] at [2007-11-19 19:45:14]

# 1 Re: dropdown list in php
Hello hobee,
Take a look at this thread: PHP: Combo in form does not update well ( http://www.dev-archive.com/forum/showthread.php?p=1332055#post1332055). It is one way of doing what you described.
Regards
# 2 Re: dropdown list in php
i have tried it but it doesnt seem to be working the way i want it to....
heres my code.
<?php
header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()-10) . " GMT");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 5) . " GMT");
header("Pragma: no-cache");
header("Cache-control: private");
$caption1 = "caption1";
$caption2 = "caption2";
$list1 = array("1-aa", "2-bb", "3-cc");
$list2_1 = array("one-a", "one-b", "one-c");
$list2_2 = array("two-a", "two-b", "two-c");
$list2_3 = array("three-a", "three-b", "three-c");
?>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<head>
</head>
<body>
<table height=450>
<tr><td>
<!---content start-->
<table width = 200>
<tr><td>
<p><p>
<p>
<form name="choice1" method="post" action = "SBChoice.php">
<?php echo $caption1;?>
<select id = "List1" onChange="document.choice1.submit();">
<?php for ($i = 0; $i <= 2; $i++) {
echo "<option>" .$list1[$i]. "</option>";
}
?>
</select>
</form>
<?php if ($_POST['choice1'] !="" ) $choice1 = $_POST['choice1']; else $choice1 = 1; ?>
<?php echo $choice1;?>
<form name="choice2" method="post" action="SBChoice.php">
<?php echo $caption2; ?>
<input type="hidden" name="choiceList2" value="<? echo $choice1; ?>" />
<select id='List2' onChange="document.choice2.submit();" >
<?php if ($choice1 = "1-aa") {
for ($i = 0; $i <= 2; $i++) {
echo "<option>" .$list2_1[$i]. "</option>";
}
} elseif ($choice1 ="2-bb") {
for ($i = 0; $i <= 2; $i++) {
echo "<option>" .$list2_2[$i]. "</option>";
}
} else { echo "failed";
}
?>
</select>
<p>
</form>
</td></tr>
</table>
<!---content end-->
</td></tr>
</table>
</body>
</html>
seems there is no value in $choice1.
please help.
Hobee
hobee at 2007-11-10 3:58:39 >

# 3 Re: dropdown list in php
Please use 'php' tags if you post some code.
this one should work
<html>
<body>
<?
$list1 = array("1", "2", "3");
$list2_1 = array("one-a", "one-b", "one-c");
$list2_2 = array("two-a", "two-b", "two-c");
$list2_3 = array("three-a", "three-b", "three-c");
?>
<form name="form1" method="post" action = "test101.php">
<select name = "choice1" onChange="document.form1.submit();">
<?
for ($i = 0; $i < 3; $i++)
{
if($list1[$i]==$_POST['choice1'])
echo "<option value='$list1[$i]' selected='selected' >" .$list1[$i]. "</option>";
else
echo "<option value='$list1[$i]'>" .$list1[$i]. "</option>";
}
?>
</select>
</form>
<select id='List2'>
<?
$choice1 = @$_POST['choice1'];
if ($choice1 == "1")
$fi_list = $list2_1;
elseif ($choice1 == "2")
$fi_list = $list2_2;
elseif ($choice1 == "3")
$fi_list = $list2_3;
else
$fi_list = $list2_1;
for ($i = 0; $i < 3; $i++)
{
echo "<option>" .$fi_list[$i]. "</option>";
}
?>
</select>
</body>
</html>
# 4 Re: dropdown list in php
thats cool!!! thanks buddy! i will play around with that.
hobee at 2007-11-10 4:00:44 >

# 5 Re: dropdown list in php
hi guys , i tried to add one more dropdown list with event handler, it ended up with resetting all the choice1 and choice2...i wanted to keep the 1st and 2nd dropdown list as the selected values. could anyone tell me what the problem is? thanks
php codes
<html>
<body>
<?
$list1 = array("1", "2", "3");
$list2_1 = array("one-a", "one-b", "one-c");
$list2_2 = array("two-a", "two-b", "two-c","two-d");
$list2_3 = array("three-a", "three-b", "three-c");
$list3 = array ("A", "B","C");
?>
<form name="form1" method="post" action = "test101.php">
<select name = "choice1" onChange="document.form1.submit();">
<?
for ($i = 0; $i < count($list1); $i++)
{
if($list1[$i]==$_POST['choice1'])
echo "<option value='$list1[$i]' selected='selected' >" .$list1[$i]. "</option>";
else
echo "<option value='$list1[$i]'>" .$list1[$i]. "</option>";
}
?>
</select>
</form>
<form name="form2" method="post" action = "test101.php">
<select name = "choice2" onChange="document.form2.submit();">
<? $choice1 = @$_POST['choice1'];
if ($choice1 == "1")
$fi_list = $list2_1;
elseif ($choice1 == "2")
$fi_list = $list2_2;
elseif ($choice1 == "3")
$fi_list = $list2_3;
else
$fi_list = $list2_1;
for ($i = 0; $i < count($fi_list); $i++)
{
echo "<option>" .$fi_list[$i]. "</option>";
}
?>
</select>
</form>
<select id='List3'>
<?
$choice1 = @$_POST['choice1'];
$choice2=@$_POST['choice2'];
if ($choice2 == "one-b")
$fi_list = $list3;
elseif ($choice2 == "two-b")
$fi_list = $list3;
elseif ($choice2 == "three-c")
$fi_list = $list3;
else
$fi_list = $list2_2;
for ($i = 0; $i < count($fi_list); $i++)
{
echo "<option>" .$fi_list[$i]. "</option>";
}
?>
</select>
</body>
</html>
hobee at 2007-11-10 4:01:40 >

# 6 Re: dropdown list in php
im sorry i dunno how to use 'php' tags. sorry about the messy code againJust put your code between [php] tag (like those HTML tags, e.g. <body>).
# 7 Re: dropdown list in php
Well in your code you have two forms 'form1' and 'form2'.
In form1 you submit 'choice1' but if you change the selection of the
2. combobox you don't submit 'choice1' because 'choice1' is in 'form1'.
In this case you can use hidden fields where you can save the value
of 'choice1' in 'from2'.
Also your for loop in 'form2' is wrong you fill only the combobox but
don't make any selection, compare it with the for loop in 'form1'.
So, here is a modified and working example
<html>
<body>
<?
$list1 = array("1", "2", "3");
$list2_1 = array("one-a", "one-b", "one-c");
$list2_2 = array("two-a", "two-b", "two-c");
$list2_3 = array("three-a", "three-b", "three-c");
$list3_1 = array ("one-a-1", "one-a-2","one-a-3");
$list3_1_2 = array ("one-b-1", "one-b-2","one-b-3");
$list3_1_3 = array ("one-c-1", "one-c-2","one-c-3");
$list3_2 = array ("two-a-1", "two-a-2","tow-a-3");
$list3_2_2 = array ("two-b-1", "two-b-2","tow-b-3");
$list3_2_3 = array ("two-c-1", "two-c-2","tow-c-3");
$list3_3 = array ("three-a-1", "three-a-2","three-a-3");
$list3_3_2 = array ("three-b-1", "three-b-2","three-b-3");
$list3_3_3 = array ("three-c-1", "three-c-2","three-c-3");
?>
<form name="form1" method="post" action = "test404.php">
<select name = "choice1" onChange="document.form1.submit();">
<?
for ($i = 0; $i < count($list1); $i++)
{
if($i==$_POST['choice1'])
echo "<option value='$i' selected='selected' >" .$list1[$i]. "</option>";
else
echo "<option value='$i'>" .$list1[$i]. "</option>";
}
?>
</select>
</form>
<form name="form2" method="post" action = "test404.php">
<select name = "choice2" onChange="document.form2.submit();">
<?
$choice1 = @$_POST['choice1'];
switch($choice1)
{
case 0:
$fi_list = $list2_1;
break;
case 1:
$fi_list = $list2_2;
break;
case 2:
$fi_list = $list2_3;
break;
$fi_list = $list2_1;
}
for ($i = 0; $i < count($fi_list); $i++)
{
if($i==$_POST['choice2'])
echo "<option value='$i' selected='selected' >" .$fi_list[$i]. "</option>";
else
echo "<option value='$i'>" .$fi_list[$i]. "</option>";
}
?>
</select>
<input type="hidden" value="<? echo $choice1 ?>" name="choice1">
</form>
<select id='List3'>
<?
$choice1 = @$_POST['choice1'];
$choice2 = @$_POST['choice2'];
switch($choice1)
{
case 0:
switch ($choice2)
{
case 0:
$fi_list = $list3_1;
break;
case 1:
$fi_list = $list3_1_2;
break;
case 2:
$fi_list = $list3_1_3;
break;
}
break;
case 1:
switch ($choice2)
{
case 0:
$fi_list = $list3_2;
break;
case 1:
$fi_list = $list3_2_2;
break;
case 2:
$fi_list = $list3_2_3;
break;
}
break;
case 2:
switch ($choice2)
{
case 0:
$fi_list = $list3_3;
break;
case 1:
$fi_list = $list3_3_2;
break;
case 2:
$fi_list = $list_3_3_3;
break;
}
break;
}
for ($i = 0; $i < count($fi_list); $i++)
{
echo "<option>" .$fi_list[$i]. "</option>";
}
?>
</select>
</body>
</html>
# 8 Re: dropdown list in php
thanks buddy!!!!! this is exactly what i want!!! thanks very much!!!!!
hobee at 2007-11-10 4:04:52 >
