I am using this ajax function to get options for two dropdown via ajax but it is not returning any output. Hi this is my ajax function :
<script>
$('#department').on('change',function(){
var department = $(this).val();
var course = $('#course').val();
if(department){
$.ajax({
type:'POST',
url:'ajaxData.php',
dataType: 'json',
cache: false,
data:{department: department, course: course },
success: function(data){
$('#head_name').html(data.head_name);
$('#email').html(data.email);
}
});
}else{
$('#head_name').html('<option value="">Select Department first</option>');
$('#email').html('<option value="">Select Department first</option>');
}
});
</script>
And this is my query code:
if(isset($_POST["department"]) && isset($_POST["course"])){
//Get all courses data
$query = $db->query("SELECT head_name, email FROM head WHERE course = '".$_POST['course']."' AND department = '".$_POST['department']."' ");
//Count total number of rows
$rowCount = $query->num_rows;
//Display result list
if($rowCount > 0){
while($row = $query->fetch_assoc()){
$temp = array('head_name' => '<option value="'.$row['head_name'].'">'.$row['head_name'].'</option>', 'email' => '<option value="'.$row['email'].'">'.$row['email'].'</option>' );
echo json_encode($temp);
}
}else{
$temp = array('head_name' => '<option value="">Not avaialble </option>', 'email' => '<option value="">Not avaialble </option>' );
echo json_encode($temp);
}
}
But I'm not getting output result from it, what I'm doing wrong ?
courseviaconsole.logafter you assigned the result of$('#course').val(). If it's not defined, you will get no response.