#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Dynamic Dependent Drop Down working - But how do I get the right column to display


    Hi

    I have 2 drop downs on a form where when you select an option from the first it automatically limits the options available on the second.

    The data for both comes from two tables:

    tblClients: ClientID, ClientName
    tblPromotions: PromoID, PromotionName, ClientID, ClientName

    At this point the drop downs are working in as much as the ClientID is showing. So if I choose 2 in the first list the second has three 2's listed.

    What I really need is to get the ClientName to appear in the first list and the subsequent PromotionName to appear in second list.

    This is where I am stuck... how do I specify which column from the table should be displayed.

    Below is the code relating to the drop downs. Any guidance would be appreciated.

    PHP Code:
    function setOptions(chosen) { 
      var selbox = document.myform.selectpromotion; 
        
      selbox.options.length = 0; 
      if (chosen == "0") { 
        selbox.options[selbox.options.length] = new Option('First select a client','0'); 
        
      } 
      <?php 
      $client_result 
    mysql_query("SELECT * FROM tblClients") or die(mysql_error()); 
      while(@(
    $c=mysql_fetch_array($client_result))) 
      { 
      
    ?> 
        if (chosen == "<?=$c['ClientID'];?>") { 
         
        <?php 
        $c_id 
    $c['ClientID']; 
        
    $promo_result mysql_query("SELECT * FROM tblPromotions WHERE ClientID='$c_id'") or die(mysql_error()); 
        while(@(
    $m=mysql_fetch_array($promo_result))) 
        { 
        
    ?> 
          selbox.options[selbox.options.length] = new 
          Option('<?=$m['ClientID'];?>','<?=$m['ClientID'];?>'); 
        <?php 
        

        
    ?> 
        } 
      <?php 
      

      
    ?> 

    </script> 
    </head> 
    <body> 
    <form name="myform"><div align="center"> 
    <select name="selectclient" size="1" 
    onchange="setOptions(document.myform.selectclient.options 
    [document.myform.selectclient.selectedIndex].value);"> 
    <option value="0" selected>Select a client</option> 
    <?php 
    $result 
    mysql_query("SELECT * FROM tblClients") or die(mysql_error()); 
    while(@(
    $r=mysql_fetch_array($result))) 

    ?> 
      <option value="<?=$r['ClientID'];?>"><?=$r['ClientID'];?></option> 
    <?php 

    ?> 
    </select><br><br> 
    <select name="selectpromotion" size="1"> 
    <option value=" " selected>First select a client</option> 
    </select><br><br> 
    <input type="button" name="go" value="Value Selected" 
    onclick="alert(document.myform.selectpromotion.options 
    [document.myform.selectpromotion.selectedIndex].value);">
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    If I understand your question correctly, you cannot accomplish what you want (server side) with PHP, If you need to control the 2nd drop down based on choices from the first (client side) you will need Javascript.
    Last edited by gw1500se; November 9th, 2012 at 07:05 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Got It


    Hi

    I actually got this to work! It was all a matter of putting 'PromotionName' and 'ClientName' in the right place.

    Here is the code I used:

    PHP Code:
    <html> 
    <head> 
    <title>Enter Winners</title> 
    <script language="javascript"> 
    function setOptions(chosen) { 
      var selbox = document.myform.selectpromotion; 
        
      selbox.options.length = 0; 
      if (chosen == "0") { 
        selbox.options[selbox.options.length] = new Option('First select a client','0'); 
        
      } 
      <?php 
      $client_result 
    mysql_query("SELECT * FROM tblClients") or die(mysql_error()); 
      while(@(
    $c=mysql_fetch_array($client_result))) 
      { 
      
    ?> 
        if (chosen == "<?=$c['ClientID'];?>") { 
         
        <?php 
        $c_id 
    $c['ClientID']; 
        
    $promo_result mysql_query("SELECT * FROM tblPromotions WHERE ClientID='$c_id'") or die(mysql_error()); 
        while(@(
    $m=mysql_fetch_array($promo_result))) 
        { 
        
    ?> 
          selbox.options[selbox.options.length] = new 
          Option('<?=$m['PromotionName'];?>','<?=$m['ClientID'];?>'); 
        <?php 
        

        
    ?> 
        } 
      <?php 
      

      
    ?> 

    </script> 
    </head> 
    <body> 
    <form name="myform"><div align="center"> 
    <select name="selectclient" size="1" 
    onchange="setOptions(document.myform.selectclient.options 
    [document.myform.selectclient.selectedIndex].value);"> 
    <option value="0" selected>Select a client</option> 
    <?php 
    $result 
    mysql_query("SELECT * FROM tblClients") or die(mysql_error()); 
    while(@(
    $r=mysql_fetch_array($result))) 

    ?> 
      <option value="<?=$r['ClientID'];?>"><?=$r['ClientName'];?></option> 
    <?php 

    ?> 
    </select><br><br> 
    <select name="selectpromotion" size="1"> 
    <option value=" " selected>First select a client</option> 
    </select><br><br> 
    <input type="button" name="go" value="Value Selected" 
    onclick="alert(document.myform.selectpromotion.options 
    [document.myform.selectpromotion.selectedIndex].value);"> 
    </div></form> 
    </body> 
    </html>

IMN logo majestic logo threadwatch logo seochat tools logo