I have a page with two selects, the first to choose the car manufacturer and the second to choose the car series. The select options should change accordingly to what the manufacturer has been chosen. Say if "BMW" is chosen then a list such as "3 Series", "Z Series", etc should show. Then they should change if I changed the manufacturer to "Audi". I have tried many attempts to pass through the data with AJAX to speak to my PHP function but upon the "success:function(data)", the data is null as seen on the console log. Below is my ajax code.
$(document).ready(function(){
$('#chooseManu').on('change',function(){
var manu = $(this).val();
if(manu){
$.ajax({
url:'ServerScripts/showStock.php?manu='+manu,
data:{'manu':manu},
dataType: 'json',
success:function(data){
// $('#chooseSeries').html(html);
console.log(data);
}
});
}
});
});
PHP Function that is to change the series options according to manufacturer chosen
function seriesChoice() {
require 'connection.php';
if (isset($_POST["manu"]) && !empty($_POST["manu"])) {
$chosenManu = json_decode($_POST["manu"]);
$findSeries = "SELECT DISTINCT carSeries FROM carParts WHERE carManu = ".$chosenManu;
$series = $conn->query($findSeries);
if ($series->num_rows > 0) {
echo '<option value="">Series</option>';
while($displaySeries = $series->fetch_assoc()) {
echo '<option>',$displaySeries['carManu'],'</option>';
}
} else {
echo '<option value="">Series Not Available</option>';
}
} else {
echo '<option value="">Select Manufacturer</option>';
}
}
?>
And then my html where it all shows:
<select id="chooseManu">
<option value="0" selected="selected">Manufacturer</option>
<?php
manuChoice();
?>
</select>
<select id="chooseSeries">
<?php
seriesChoice();
?>
</select>
In the webdev tools (F12), on the 'Network' tab, I am able to see that it does pass 'ServerScripts/showStock.php?manu=(manu chosen)', but on the console log the data shows as null. Unless I "console.log(manu)", then obviously it logs the manufacturer chosen also. I have tested with many statements to see if the $_POST in the php function is not empty and isset, neither are true. So it looks like the function isn't even seeing the variable being passed through at all.
Understanding this is one of my first actual projects, I can see that there will be a lot of things here that aren't nice to see in the coding world, so please be gentle with me! However, my major concern here at the moment is getting this variable to be seen.
If there is anything here I have not explained well or you just need clarification with please do let me know!
Any help is greatly appreciated!