×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

FORMS

Dynamic Dropdown by vbkris
Posted: 11 Nov 03

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...

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close