Mark Alan Mark Alan - 2 months ago 6
PHP Question

Data not returning Ajax

I am having issue with retuning php data in the form of array I have create an ajax query to show data on 2 div 1 is drop down and second is the div where all data wil be shown but the problem is that whEN i TRY to do so teh data all the data shows up in drop down and nothing effects second div can anyone help me out please

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


Here is the 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();
}

Answer

Remove echo "<option> --- Select Location --- </option>"; to get valid json :)