I have been searching online for a solution and none of them seem to work, I am trying to filter the options in a combobox which is supposed populated using an sql query that filters uding the entry of another combobox.
I have been able to populate the combobox from the sql database using php but I cant filter the results using the first combobox.
My coding is below;
Query code
<?php
$connection = mysqli_connect("", "", "", "");
$lab = mysqli_real_escape_string($_POST['LabDep']);
$results = mysqli_query($connection, "SELECT SampleID, SampleType FROM `sampletypes` WHERE Lab = $lab");
mysqli_close($connection);
?>
Combobox code
<select input name="SampType1">
<?php foreach($results as $user): ?>
<option value="<?= $user['SampleID']; ?>"><?= $user['SampleType']; ?></option>
<?php endforeach; ?>
</select style="visibility:hidden;"/>
AJAX code made from I don't know how many online guides so probably a real hash as I haven't used this before
<script>
function GetNames(LabDep) {
if (genderID > 0) {
$("#SampType1").get(0).options.length = 0;
$("#SampType1").get(0).options[0] = new Option("Loading samples", "-1");
$.ajax({
type: "POST",
url: "",
data: "{Lab:" + Lab + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#SampType1").get(0).options.length = 0;
$("#SampType1").get(0).options[0] = new Option("Select name", "-1");
$.each(msg.d, function(index, item) {
$("#SampType1").get(0).options[$("#SampType1").get(0).options.length] = new Option(item.Display, item.Value);
});
},
error: function() {
$("#SampType1").get(0).options.length = 0;
alert("Failed to load names");
}
});
}
else {
$("#SampType1").get(0).options.length = 0;
}
}
</script>
there will end up being 4 comboboxes that will need updating, sampletype1, sampletype2, sampletype3 and sampletype4, this will also only be visible when certain quantities are selected (already coded and working)
It is so frustratingly close to working, I just dont know where I am going wrong.
Thank you all in advance for your help.