Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Dropdown

FORMS

Dynamic Dropdown

by  vbkris  Posted    (Edited  )
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>


hope this helps...
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top