简体   繁体   English

通过jQuery Ajax创建的下拉列表:如何在第一个下拉列表中的选定值的第二个下拉列表中获取相关项

[英]Dropdown created via jquery ajax:how to get related items in second dropdown of selected value in first dropdown

I m trying to fill 2nd dropdown but its not filling with filter data but it fetches all states rather only related states according to country selected in first dropdown Please tell me whats wrong in code.Value of id cannot be found so all states name are filled in dropdown . 我正在尝试填充第二个下拉列表,但未填充过滤器数据,但它会提取所有州,而仅根据第一个下拉列表中选择的国家/地区获取相关州。请告诉我代码中出了什么问题。找不到ID的值,因此所有州名都已填写在下拉菜单中。

               <script type="text/javascript" src="jquery-ajax/jquery.js"></script>
<script>
$(document).ready(function(){             

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

        //var optionValue = $("select[name='country_select']").val();      
       var id=$("#country").val(); 
        //var dataString = 'id='+ id;          
          //alert("datastring"+dataString); 

        $.ajax({
            type: "POST",
            url: "getstate.php",
            data: {Country_Id:id},
            beforeSend: function(){ $("#ajaxLoader").show(); },
            complete: function(){ $("#ajaxLoader").hide(); },
            success: function(response){

                $("#stateAjax").html(response);
                $("#stateAjax").show();

        }

        });        
    });

});
</script>

</head>
<?php
include("connection.php");
?>
<body>
<form method="post">
Select Country:<select name="country_select" id="country">
    <option value="">Select Country</option>
<?php
$cntry = mysql_query("SELECT Country_Id, name FROM country ORDER BY name ASC");
while($row = mysql_fetch_array($cntry))
{
    $id=$row['Country_Id'];
    $name=$row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
?>
    </select>
 <span id="errmsg" style="display:none">There is no matching option</span>
  <div id="ajaxLoader" style="display:none"><img src="jquery-ajax/ajax-loader.gif" alt="loading..."></div>
  <div id="stateAjax" style="display:none">
  <select name="state_select" id="state" style="display:none">
  <option value="">Please Select</option></select></select>
  </div>
</form>   
</body>

        getstate.php


    <?php

    echo $_POST['id'];

    include("connection.php");

    $cntry = mysql_query("SELECT * FROM state where country_id=".$_POST['id']);
    ?>
     State: 
    <select name="state_select" id="state">
    <option value="">Please Select</option>
    <?php
    while($row = mysql_fetch_array($cntry))
    {
        $id=$row['State_id'];
        $name=$row['name'];
    echo '<option value="'.$id.'">'.$name.'</option>';

    }

    ?>
    </select>
$.ajax({
                type: "POST",
                url: "getstate.php",
                data: {country_id:id},
                ...

Edit 1: also 编辑1:也

var id=$('#country').val();

Edit 2: getstate.php 编辑2:getstate.php

include("connection.php");

$country_Id = intval($_POST['Country_Id']);//if country_Id is integer...if not:$country_Id = $_POST['Country_Id'];
$cntry = mysql_query("SELECT * FROM state where country_id=".$country_Id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在第一个下拉菜单中选择了某个值,如何显示第二个下拉菜单? - How to display a second dropdown menu if a certain value in the first dropdown is selected? 使用jQuery获取2个下拉菜单项的选定值 - Get selected value of a 2 dropdown items using jQuery 如何根据第一个下拉框中选定的值在下拉框中获取第二个值 - How to get the second value in a dropdown box based on the selected one in the first dropdown box 如果在第一个下拉列表中选择了一个值(使用变量),则显示第二个下拉列表? - Display second dropdown list if a value is selected in the first dropdown (using variables)? 在文本框中获取与下拉列表相关的选定值 - Get the selected dropdown related value into the text box 使用jQuery获取动态创建的多个下拉列表的选定值 - Get selected value of dynamically created multiple dropdown using jquery 如何获得选定的下拉值? - How to get selected dropdown value? 如何使用jQuery / AJAX和PHP / MySQL根据选择的第一个下拉列表填充第二个下拉列表? - How to populate second dropdown based on selection of first dropdown using jQuery/AJAX and PHP/MySQL? 如何使第二个下拉值与第一个下拉值相同? - How to make second dropdown value the same as the first dropdown value? 如何获取通过echo创建的下拉列表的选定值? - How can I get the selected value of a dropdown list created via echo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM