I am trying to create a cost estimation form in WordPress using custom code and I am a beginner in PHP, AJAX, and MYSQL so I need help in the below query.
Basically need to create a dependent dropdown for Car Make and Car Model so I have created one dab table call wp_cost_estimation which has id, make, make_id, model. See the below image.
So if the user selects Acura in the next drop down it should show all the Acura models without repetition. I tried the below code. Please correct me where I am wrong.
cost_estimation_template.php
<select placeholder="Select Make" name="make" id="make">
<option disabled="" selected="">--Select Make--</option>
<?php
include get_theme_file_path( '/data.php' );
$makes = loadMake();
foreach ($makes as $make) {
echo "<option id='".$make['id']."' value='".$make['id']."'>".$make['make']."</option>";
}
?>
</select>
<select placeholder="Select Model" name="model" id="model">
<option disabled="" selected="">--Select Model--</option>
</select>
data.php
if(isset($_POST['aid'])) {
$db = new DbConnect;
$conn = $db->connect();
$stmt = $conn->prepare("SELECT model_name FROM wp_cost_estimation WHERE make_id = " . $_POST['aid'] GROUP BY model_name);
$stmt->execute();
$models = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($models);
}
function loadMake() {
$db = new DbConnect;
$conn = $db->connect();
$stmt = $conn->prepare("SELECT MIN(id) AS id, make FROM wp_cost_estimation GROUP BY make");
$stmt->execute();
$make = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $make;
}
AJAX Code:
<script type="text/javascript">
$(document).ready(function(){
$("#make").change(function(){
var aid = $("#make").val();
$.ajax({
url: '<?php echo get_theme_file_path('/data.php'); ?>',
method: 'post',
data: 'aid=' + aid
}).done(function(model){
console.log(model);
model = JSON.parse(model);
$('#model').empty();
model.forEach(function(models){
$('#model').append('<option>' + models.model_name + '</option>')
})
})
})
})
</script>
Please help me out.


get_theme_file_path('/data.php'). Could you please show what return this function?