Dynamic Drop Down Menus

Discussion in 'PHP' started by SiteExpress, Nov 22, 2006.

  1. #1
    I am creating a dynamic drop down menu that used 2 Select boxes pulling data from a MyAQL db.

    Example:

    The first menu works fine and is populated by subjects and is coded sonething like this.

    <select name="subject">
     <option selected="selected">Select One</option>
     <? echo $options ?>
     </select>
    Code (markup):
    The second menu is where I run into problems. I would like it to dynamically change, based on the selection in the first menu. here is what I have coded for it.

    <select name="program">
          <option selected="selected">Select One</option>
          <? echo $programs ?>
        </select>
    Code (markup):
    The php I have used for both is below.

    <?php require_once('admin/config/config.php');
     
     $result = mysql_query("SELECT * FROM subjects") or die(mysql_error());
     $options="";
     while($row = mysql_fetch_array($result)){
     $subject=$row["subject"];
     $options.="<option>".$subject.'</option>';
     }
     
     $result = mysql_query("SELECT * FROM subjects WHERE subject='$subject'") or die(mysql_error());
     $programs="";
     while($row = mysql_fetch_array($result)){
     $program=$row["program"];
     $programs.="<option>".$program.'</option>';
     }
     ?>
    Code (markup):
    I understand this may require some Jave to complete, but since I am only just learning php and MySQL, I dont think this is something I can do alone. Any help is appreciated, and I am willing to pay a little if someone could get this done.

    Thanks!
     
    SiteExpress, Nov 22, 2006 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    the second box wont get populated because the $subject you are passing to it is server-side. you will need to submit the first select box so the PHP can process the SQL.

    here is a start. this example will reset the first box back to default though:
    
    <select name="subject" onChange="location.href='pageName.php?subject='+this.value;">
    
    Code (markup):


    *Edit - i forgot the = between subject and this.value
     
    lbalance, Nov 22, 2006 IP
  3. SiteExpress

    SiteExpress Well-Known Member

    Messages:
    1,355
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    155
    #3
    Thanks for the reply. I think I see what you are getting at. I am attempting a piece of js code, but if it does not work out, I will go with your suggestion.
     
    SiteExpress, Nov 22, 2006 IP