Not sure what I'm doing wrong here but I have the following AJAX function. console.log(make_id);
is working fine, however data: {make:make_id},
doesn't seem to pass to /get_models.php - this line of code $make_id = $POST['make'];
$(document).ready(function() {
$("#select_make").change(function() {
var make_id = $(this).val();
console.log(make_id);
$.ajax({
url: '/car_clearance_form/get_models.php',
type: 'post',
data: {
make: make_id
},
dataType: 'json',
success: function(response) {
console.log(response);
var len = response.length;
$("#select_model").empty();
for (var i = 0; i < len; i++) {
var model_id = response[i]['model_id'];
var model_name = response[i]['model_name'];
$("#select_model").append("<option value='" + model_id + "'>" + model_name + "</option>");
}
}
});
});
});
This is the code for get_models.php
I am getting the following error PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
. $sql
seems to be returning false, as $make_id
isn't getting passed any thing from the AJAX function
<?php
include "config.php";
$make_id = $POST['make'];
$sql = "SELECT model_name, model_id FROM models WHERE make_id = ".$make_id;
$result = mysqli_query($con, $sql);
$models_array = array();
while ($row = mysqli_fetch_array($result) ) {
$model_id = $row['model_id'];
$model_name = $row['model_name'];
$models_array = array("model_id" => $model_id, "model_name" => $model_name);
}
// encoding array to JSON format
echo json_encode($models_array);
?>