Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...The level of expertise is awesome. The nature in which people respond is professional helpful and not the least condescending. I can't say that for most forums..."

Geography

Where in the world do Tek-Tips members come from?

FORMS

Dynamic Dropdown
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