数据没有返回Ajax

时间:2016-09-11 20:19:44

标签: javascript php jquery html ajax

我遇到了以数组形式重新调整php数据的问题我已经创建了一个ajax查询来显示2 div 1上的数据是下拉,第二个是div所有数据都将显示但是问题是那个我尝试这样做数据所有数据显示在下拉列表中没有任何影响第二个div可以帮助我任何人

$(document).ready(function() {
$("#city").on('change', function() {
    var city = $('#city option:selected').text();

    $(document).ajaxStart(function() {
        $('#wait').css('opacity', '1');
    });

    $(document).ajaxComplete(function() {
        $('#wait').css('opacity', '0');
    });

    $.ajax({
        type     :  "POST",
        data     :  {city: city, command: 'city'},
        dataType :  'JSON',
        url      :  "includes/get_data.php",
        success  :  function(data) {
            $("#location").html(data.city);
            $("#leads").html(data.lead);
        }
    });
});
});

这是get_data.php

if(isset($_POST["command"]) && $_POST['command'] == 'city') {
    $sql = mysqli_query($connection, "SELECT DISTINCT location FROM doctors WHERE city = '".$_POST['city']."'");
    echo "<option> --- Select Location --- </option>";
    while($data1 = mysqli_fetch_assoc($sql)) {
        $data['city'] = "<option";
            if(isset($_POST['location']) == $data1['location']) {
                $data['city'] .= " selected";
            }
        $data['city'] .= ">".$data1['location']."</option>";
    }

    $sql2 = mysqli_query($connection, "SELECT * FROM doctors WHERE city = '".$_POST['city']."'");

    if(mysqli_num_rows($sql2) >= 1) {
        while($data2 = mysqli_fetch_assoc($sql2)) {

        $data['lead']  = '<div class="postings doctor">';
        $data['lead'] .= '<div class="col-md-9"><div class="inner">';
        $data['lead'] .= '<h2>'.$data2['name'].'</h2>';
        $data['lead'] .= '<span>'.$data2['field'].'</span>';
        $data['lead'] .= '<p><'.$data2['qualification'].'</p>';
        $data['lead'] .= '<ul><li><span>Address</span>'.$data2['address'].'</li>';
        $data['lead'] .= '<li><span>Fee Range</span>'.$data2['fee_range'].'</li></ul></div></div>';
        $data['lead'] .= '<div class="col-md-3"><a href="details.php?id='.$data2['id'].'">View Details</a></div><div class="clearfix"></div></div>';
        }
    }
    echo json_encode($data);
    exit();
}

1 个答案:

答案 0 :(得分:1)

删除echo "<option> --- Select Location --- </option>";以获取有效的json:)