this is in response to questions regarding dynamic dropdown menus.
lets say we have a dropwdown in a page called test.php, with a form called FormName:
<select>
<option value="1">1</option>
<option value="2">2</option>
</select>
now when the user selects 1 it must populate a second dropdown with values related to 1.
now i am going to change the code a little bit:
<script>
function resubmit()
{
document.FormName.action="test.php"
document.FormName.submit()
}
</script>
<select name="drp1" onchange="resubmit()">
<option value="1">1</option>
<option value="2">2</option>
</select>
FormName is the form name.
now we have to recieve the value
<?
$vl="";
if(isset($_POST['drp1']))
{
$vl=$_POST['drp1'];
$sql="select * from table where id='$vl'";
$rs=mysql_query($sql);
}
?>
<script>
function resubmit()
{
document.FormName.action="test.php"
document.FormName.submit()
}
</script>
<select name="drp1" onchange="resubmit()">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="drp1">
<?
if($vl!="")
{
//Some value has been selected in box1
for($i=0;$i<mysql_num_rows($rs);$i++)
{
$row=mysql_fetch_row($rs);
echo "<option value='$row[0]'>$row[1]</option>";
}
}
?>
</select>
the above is will populate drp2 with the required values, now the selected value in drp1 must remain same as selected before the form has been submitted, for that:
<?
$vl="";
if(isset($_POST['drp1']))
{
$vl=$_POST['drp1'];
$sql="select * from table where id='$vl'";
$rs=mysql_query($sql);
}
?>
<script>
function resubmit()
{
document.FormName.action="test.php"
document.FormName.submit()
}
</script>
<select name="drp1" onchange="resubmit()">
<option value="1" <?=$vl=="1":"selected":""?>>1</option>
<option value="2" <?=$vl=="2":"selected":""?>>2</option>
</select>
<select name="drp1">
<?
if($vl!="")
{
//Some value has been selected in box1
for($i=0;$i<mysql_num_rows($rs);$i++)
{
$row=mysql_fetch_row($rs);
echo "<option value='$row[0]'>$row[1]</option>";
}
}
?>
</select>
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.