根据1st的结果填充第二个下拉列表

时间:2011-07-17 11:42:25

标签: php jquery

目前我使用动态选择来填充下拉列表。我想要做的是显示第二个下拉列表,其结果基于第一个选择。我不知道如何解决这个问题并寻找答案,但无济于事。我已经包含了用于填充第一个菜单的代码,如果您需要任何其他代码,请告诉我。如果有人可以帮助代码,我非常愿意看jQuery或javascript。非常感谢

<form id="boxform" method="post" class="webform" name="boxform" />

        <label for="company">Select a Company:</label>
                <select name="company" id="company" />
                    <option SELECTED VALUE="">Select a Company</option>
                        <?php
                          do {  
                        ?>
                    <option value="<?php echo $row_Recordsetcust['customer']?>"><?php echo $row_Recordsetcust['customer']?></option>
                        <?php

                    } 
                        while ($row_Recordsetcust = mysql_fetch_assoc($Recordsetcust));
                        $rows = mysql_num_rows($Recordsetcust);

                if($rows > 0)

                    {
                        mysql_data_seek($Recordsetcust, 0);
                        $row_Recordsetcust = mysql_fetch_assoc($Recordsetcust);
                    }

                         ?>
                 </select>

更新:

这是我目前拥有的第二个php格式下载代码,如果它有助于继续这一点,谢谢

<label for="boxrtnaddress">Select Address</label>
     <select name="boxrtnaddress" id="boxrtvaddress" />
        <option SELECTED VALUE="">Select Delivery Address</option>

            <?php
            while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2))
            {  
                    $value=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com'];
                    $caption=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com'];
                    echo "<option value=\"", $value, "\">", $caption, "</option>";
            }
            ?>
    </select>

+++++++++++++ SOLUTION ++++++++++

<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script language="javascript" type="text/javascript">
      $(function() {
            $("#company").change(function() {
              if ($(this).val()!="") $.get("getOptions.php?customer=" + $(this).val(), function(data) {
                $("#divId").html(data);
                });
              });
      });
</script>

getOptions.php

<?php
    $customer = mysql_real_escape_string( $_GET["customer"] ); // not used here, it's the customer choosen

    $con = mysql_connect("localhost","root","");
    $db = "sample";
      if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($db, $con);
        $query_rs_select_address2 = sprintf("SELECT * FROM company_com where idcode_com = '$customer'");
        $rs_select_address2 = mysql_query($query_rs_select_address2, $con) or die(mysql_error());
        $row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2);
        $totalRows_rs_select_address2 = mysql_num_rows($rs_select_address2);



                    $address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com'];
                     echo '<select name="customer">'.'<option value="">Select delivery address</option>'.'<option value="address">'.$address.'</option>'.'</select>';



?>

1 个答案:

答案 0 :(得分:1)

它可能有所帮助,这里是一个小提琴

http://jsfiddle.net/gZzQr/

根据第一个select的选择动态附加select代码是乱的和脏的所以我不会在这里发布它:)

您可以查看以下内容以更好地了解正常工作

select的选定值发生变化时,

change会触发

is(':empty')检查新创建的`选择的div  将附加 为空或不是

http://api.jquery.com/is/

http://api.jquery.com/empty/

修改 我会给你提示,希望你能填写剩下的代码......

首先,您需要做的是处理第一次下拉列表中的change事件,当值发生变化时,您可以执行ajaxget,{{1}请求到您的服务器,获取结果并填充第二个下拉列表,这是一个有用的stackoverflow链接,可以帮助您

php dropdown menu population

post

在你的results.php

从第一次下拉中获取值

$("#firstDD").change(function(){
var value = $(this).val();//get the changed value of first dd(drop down)


//now do a post request 

$.post("results.php",{data:value},function(data){
//get the values here and populate the second drop down

});

});

这里有一些有用的链接

http://www.prodevtips.com/2008/08/15/jquery-json-with-php-json_encode-and-json_decode/

http://api.jquery.com/jQuery.post/

http://www.factsandpeople.com/facts-mainmenu-5/26-html-and-javascript/89-jquery-ajax-json-and-php