how to using javascript to create two level drop down list

Discussion in 'JavaScript' started by mmjanjust, Oct 6, 2010.

  1. #1
    hi all,
    i want to create two level drop down list , and the data are retrive from mysql ,

    the first and the second drop down list data are all from the same table of my db,
    for example :
    data are a, b,c,d
    1.when user select 'a' from select_1 then select_ 2 only shown b,c,d
    2.when user select 'b' from select_1 then select_ 2 only shown a,c,d
    3......
    anyone knows how to do ?
     
    mmjanjust, Oct 6, 2010 IP
  2. Deu

    Deu Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #2
    first write the html markup for them, and add onchange in 1st drop down like this

    <select onchange="javascript:FunctionToCallServerBack(withSelectedItem) ..........

    then do the DB process and re-render the second dropdown
     
    Deu, Oct 6, 2010 IP
  3. mmjanjust

    mmjanjust Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    it's my code , but still can't show drop down list for select_2 , what's wrong with my code ?

    <script>
    function buildSelect2($select1)
    {
    <?
    echo "<td>";
    $sql = "select addr from customer where addr <>$select1";
    echo "<select name=\"select2\" size=\"0\" >" ;
    for ($i=0;$i<$result_num;$i++)
    {
    $row = mysql_fetch_array($result);
    $addr= $row['addr'];
    echo "<option value=$addr>$addr</option>";

    }
    echo "</td>";
    ?>

    }
    </script>
    ..................
    echo "<td>";
    echo "<select name=\"select1\" size=\"0\" onchange = buildSelect2(this) >" ;
    for ($i=0;$i<$result_num;$i++)
    {
    $row = mysql_fetch_array($result);
    $addr= $row['addr'];
    echo "<option value=$addr>$addr</option>";

    }
    echo "</td>";
     
    mmjanjust, Oct 6, 2010 IP