【问题标题】:Drop down menu with jquery, php, mysql带有 jquery、php、mysql 的下拉菜单
【发布时间】:2014-04-12 02:03:48
【问题描述】:

我是编程新手,所以如果您能提供解释以便我可以边学习边学习,将不胜感激。

好的,所以我正在从 sql 表中创建一个下拉菜单,并且我正在使用 php 和 Jquery。到目前为止,我已经得到了我的第一个子菜单,它是从我的国家菜单中填充的州。现在我对如何让我的城市菜单从我的状态菜单中填充感到困惑。

这是我的主要 php 文件!

 <script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#flip").click(function() {
            jQuery("#panel").slideToggle("slow");
        });

        jQuery("#country").change(function() {
            //jQuery("#address").val(jQuery("#courseid :selected").val());
            var querystr = 'countryid='+jQuery('#country :selected').val();
            jQuery.post("<?php echo plugins_url(); ?>/CountryStateCity Drop Down/ajax.php", querystr, function(data){
                if(data.errorcode == 0){
                    jQuery('#statecbo').html(data.chtml)
                    //jQuery('#citydescr').append('<textarea name="citydescr" id="citydescr" cols="80" rows="3" maxlength="500"></textarea>')
                }else{
                    jQuery('#statecbo').html(data.chtml)
                }
            }, "json");
        });
    });
</script>

<html>
    <head>
        <title>Dynamic Drop Down Menu</title>

    </head>
    <body>
        <div class="wrap">
        <h5> Country</h5>
         <select id="country" name="country" required>
             <option value="">--Select Country--</option>
            <?php
            $sql=mysql_query("SELECT * from country order by name");
            while ($row=mysql_fetch_array($sql)) {
                $countryID=$row['IDCountry'];
                $countryname=$row['name'];
                echo "<option value='$countryID'>$countryname</option>";
             }
             ?>
             </select>
         </div>
        <h5>State</h5>
        <div class="wrap"  id="statecbo">



        </div>

        <div class="wrap">
             <h5>City</h5>

    </div>
    </body>
   </html>

这是我的 ajax.php 文件

$country_id = isset($_POST['countryid']) ? $_POST['countryid'] : 0;
if ($country_id <> 0) {
$errorcode = 0;
$strmsg = "";
$sql="SELECT * from state WHERE IDCountry = ". $country_id . " ORDER BY name;";
$result=mysql_query($sql);
$cont=mysql_num_rows($result);
if(mysql_num_rows($result)){
    $chtml = '<select name="states" id="states"><option value="0">--Select State--     </option>';
    while($row = mysql_fetch_array($result)){
        $chtml .= '<option value="'.$row['IDState'].'">'.$row['name'].'</option>';
    }
    $chtml .= '</select>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$chtml));
}else{
    $errorcode = 1;
    $strmsg = '<font style="color:#F00;">No States available</font>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$strmsg));
}


}

所以我的下一步是添加一个城市菜单,该菜单由我刚刚从国家菜单中填充的州菜单填充。对不起,如果这令人困惑。谢谢!

根据 off jeroen 的回复,我添加了以下内容以尝试获取城市下拉菜单。

我的主 php 文件--

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#flip").click(function() {
            jQuery("#panel").slideToggle("slow");
        });

        jQuery("#country").change(function() {

            var querystr = 'countryid='+jQuery('#country :selected').val();
            jQuery.post("<?php echo plugins_url(); ?>/CountryStateCity Drop Down/ajax.php", querystr, function(data){
                if(data.errorcode == 0){
                    jQuery('#statecbo').html(data.chtml)

                }else{
                    jQuery('#statecbo').html(data.chtml)
                }
            }, "json");
        });
        jquery(".wrap").on('change', '#states',function() {
            var querystr = 'stateid=' +jQuery('#states :selected').val();
            jquery.post("<?php echo plugins_url(); ?>/CountryStateCity Drop Down/ajax.php", querystr, function(data) {
                if(data.errorcode ==0){
                    jQuery('#citycbo').html(data.chtml)
                }else{
                    jQuery('#citycbo').html(data.chtml)
                }
            }, "json");
        });
    });
</script>

还有我的 ajax.php 文件

$country_id = isset($_POST['countryid']) ? $_POST['countryid'] : 0;
if ($country_id <> 0) {
$errorcode = 0;
$strmsg = "";
$sql="SELECT * from state WHERE IDCountry = ". $country_id . " ORDER BY name;";
$result=mysql_query($sql);
$cont=mysql_num_rows($result);
if(mysql_num_rows($result)){
    $chtml = '<select name="states" id="states"><option value="0">--Select State--</option>';
    while($row = mysql_fetch_array($result)){
        $chtml .= '<option value="'.$row['IDState'].'">'.$row['name'].'</option>';
    }
    $chtml .= '</select>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$chtml));
}else{
    $errorcode = 1;
    $strmsg = '<font style="color:#F00;">No States available</font>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$strmsg));
}


}

$state_id = isset($_POST['IDState']) ? $_POST['IDState'] : 0;
if ($state_id <> 0) {
$errorcode = 0;
$strmsg = "";
$sql="SELECT * from state WHERE IDState = ". $state_id . " ORDER BY name;";
$result=mysql_query($sql);
$cont=mysql_num_rows($result);
if(mysql_num_rows($result)){
    $chtml = '<select name="city" id="city"><option value="0">--Select city--  </option>';
    while($row = mysql_fetch_array($result)){
        $chtml .= '<option value="'.$row['IDCity'].'">'.$row['name'].'</option>';
    }
    $chtml .= '</select>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$chtml));
}else{
    $errorcode = 1;
    $strmsg = '<font style="color:#F00;">No city available</font>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$strmsg));
}


}

【问题讨论】:

  • mysql_ 函数已弃用。您应该改用mysqli_PDO。此外,您的脚本对 SQL 注入开放。
  • 如何在可编辑的文本框中显示上一次下拉选择的结果?

标签: php jquery mysql


【解决方案1】:

您对国家/州所做的基本上与您所做的相同。

但是,您使用的是 jQuery change() 函数,因此该函数无法与注册该函数时页面上不存在的元素一起使用。

您可以通过使用事件委托来解决这个问题:

jQuery(".wrap").on('change', '#states', function() {
  // do your stuff
}

我使用了 .wrap 元素,因为它是包装表单元素的元素,但您也可以使用 document 例如。

当然,你也可以对已有的使用相同的方法,只需更改:

jQuery("#country").change(function() {

到:

jQuery(".wrap").on('change', '#country', function() {

【讨论】:

  • 好的,感谢您到目前为止的帮助,但我仍然无法填充我的城市菜单。老实说,我不确定我在做什么,我花了很长时间才让我的前两个菜单正常工作。如果您仍然愿意提供帮助,我将更新我添加的原始代码。
  • 我将如何从上一个下拉菜单中将结果显示在可编辑的文本文件中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 2014-11-30
相关资源
最近更新 更多