dongping4273 2018-08-27 10: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 10: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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab