dongping4273 2018-08-27 02:06
浏览 65
已采纳

如何让json将数据编码到php的下拉字段中?

<script>
    $(document).ready(function(){
        $("#region").change(function(){
            region = $(this).val()
            $.ajax({
                type:"POST",
                data:{"region":region},
                url:"get-city.php",
                success:function(data){
                    alert(data);
                    //$("#city").html(data);
                    //$("#state").html(data);
                }
            });
        });
    });
</script>

<select name="region"  id="region" >
    <option value="">Select Region</option>
    <?php 
        $sql = mysqli_query($con, "SELECT * FROM `region`");
        while($row = mysqli_fetch_array($sql)) 
        {
    ?>
            <option value="<?php echo $row['heading_text']; ?>"><?php echo $row['heading_text']; ?></option>
    <?php 
        } 
    ?>
</select>
<select name="state" id="state">
    <option value="">Select Region State</option>
</select>
<select name="city" id="city">
    <option value="">Select Region City</option>
</select>

get-city.php

<?php 
    error_reporting(0);
    include('dbase.php');
    $region = $_POST['region'];
    $sql = "select * from region_data where heading_text='".$region."'";
    $results = mysqli_query($con,$sql);
    while($rows = mysqli_fetch_assoc($results))
    {
        $data[] = array(
                            'state' => $rows['state'],
                            'city' => $rows['name']
                        );
    }
    echo json_encode($data);
?>

In this code I have created simple dropdown one is for region where I change its value by id as you can see in jquery code. Now, As you can see in get-city.php file I have encode data via json_encode function which work fine. But problem is I am not able to show data in state and city drop down in php. My json_encode data look like:

[{"state":"","city":"delhi"},{"state":"","city":"agra"},{"state":"","city":"varanasi"},{"state":"","city":"haridwar"},{"state":"","city":"dharamshala"},{"state":"","city":"srinagar"},{"state":"","city":"mussoorie"},{"state":"","city":"amritsar"},{"state":"","city":"shimla"},{"state":"","city":"kullu manali"},{"state":"","city":"assam"},{"state":"","city":"meghalaya"},{"state":"","city":"arunachal pradesh"},{"state":"","city":"manipur"},{"state":"","city":"nagaland"},{"state":"","city":"J AND K"},{"state":"38","city":"Saharanpur"}]

So, How can I get value in dropdown? Please help me.

Thank You

展开全部

  • 写回答

4条回答 默认 最新

  • douxie1957 2018-08-27 02:21
    关注

    If you need to get back a JavaScript object from a JSON string, you are looking for JSON.parse(). I did not got exactly how you want to display data, but as far as I understand you are looking for a way to populate the dropdowns, so I will show you only for the city part...for all the other dropdowns the flow is the same.

    Assuming you have all the cities in the cities variable after the AJAX call (and after the JSON.parse call if needed):

    let cities = [{"state":"","city":"delhi"},{"state":"","city":"agra"},{"state":"","city":"varanasi"},{"state":"","city":"haridwar"},{"state":"","city":"dharamshala"},{"state":"","city":"srinagar"},{"state":"","city":"mussoorie"},{"state":"","city":"amritsar"},{"state":"","city":"shimla"},{"state":"","city":"kullu manali"},{"state":"","city":"assam"},{"state":"","city":"meghalaya"},{"state":"","city":"arunachal pradesh"},{"state":"","city":"manipur"},{"state":"","city":"nagaland"},{"state":"","city":"J AND K"},{"state":"38","city":"Saharanpur"}];
    
    cities.map(function(item, index) {
      // let's build an <option> element
      const city = jQuery("<option>", {value: item.city, text: item.city})
      // let's add the <option> element to the right <select>
      jQuery("#city").append(city);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部