Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有jquery、php、mysql的下拉菜单_Php_Jquery_Mysql - Fatal编程技术网

带有jquery、php、mysql的下拉菜单

带有jquery、php、mysql的下拉菜单,php,jquery,mysql,Php,Jquery,Mysql,我对编程相当陌生,所以如果您能提供解释,以便我边走边学习,我将不胜感激 好的,我正在从sql表中创建一个下拉菜单,我正在使用php和Jquery。到目前为止,我已经得到了我的第一个子菜单,这是国家填充从我的国家菜单。现在我对如何让我的城市菜单从我的州菜单中填充感到困惑 这是我的主要php文件 <script type="text/javascript"> jQuery(document).ready(function(){ jQuery("#flip").c

我对编程相当陌生,所以如果您能提供解释,以便我边走边学习,我将不胜感激

好的,我正在从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));
}


}
$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));
}


}
因此,我的下一步是添加一个城市菜单,该菜单由我刚刚从国家菜单填充的状态菜单填充。抱歉,这让人困惑。谢谢

根据非杰罗恩的反应,我添加了一些内容,试图获得城市下拉菜单

我的主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));
}


}
$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));
}


}

你基本上和你在国家/州做的事情一样

但是,您使用的是jQuery change函数,因此它不会处理注册该函数时不在页面上的元素

您可以通过使用事件委派来解决此问题:

jQuery(".wrap").on('change', '#states', function() {
  // do your stuff
}
我使用了.wrap元素,因为它是包装表单元素的元素,但您也可以使用document作为示例

当然,你也可以对你已经拥有的东西使用同样的方法,只要改变一下:

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


mysql_uu函数已弃用。您应该改用mysqli_uu或PDO。另外,您的脚本对SQL注入非常开放。如何在可编辑的文本框中显示上次下拉选择的结果?好的,谢谢您的帮助,但我仍然无法填充我的城市菜单。老实说,我不确定我在做什么,我花了很长时间才让我的前两份菜单正常工作。如果您仍然愿意帮助,我将在我添加的原始代码中添加更新。我将如何在上一个下拉菜单的可编辑文本文件中显示结果?