I currently have 2 multiselect dropdowns on a page, one called 'hazards' and the other 'hp_codes'. They both populate their lists from a SQL DB and the idea is that 'hp_codes' will populate based on what is selected in 'hazards'.
So in theory someone could select hazard1, hazard2, and hazard3 and then the second list will churn out the options linked to those 3 hazards (some options may be the same - duplicates to be removed).
I'm a bit of a newby to this, hence throwing a net out, so I'd any suggestions are absolutely appreciated.
So far, I've tried the solution on this question(Bootstrap multiselect dynamic options based on first dropdown) to no avail (I typed all the options in to stop the page from pulling the hp_codes from the DB. Having the ability to keep these being pulled from the DB would be really helpful for updates in the future from a maintenance page point of view.
What would be the best way to achieve this?
Edit Here is a snippet of what currently sits in my form. To pull the data in to the multiselect from the DB
// Method to bind Dropdown
function fill_hazards_select_box($con)
{
$output = '';
$query=mysqli_query($con,"SELECT hazards FROM Chemilist_states");
$cnt=1;
while($row=mysqli_fetch_array($query))
{
$output .= "<option value='$row[0]'> $row[0] </option>";
}
return $output;
}
function fill_hp_codes_select_box($con)
{
$output = '';
$query=mysqli_query($con,"SELECT hp_codes FROM Chemilist_hazards");
$cnt=1;
while($row=mysqli_fetch_array($query))
{
$output .= "<option value='$row[0]'> $row[0] </option>";
}
return $output;
}
$month = date('m');
$day = date('d');
$year = date('Y');
$today = $year . '-' . $month . '-' . $day;
?>
Then to initialise the plugins
<script>
$(document).ready(function() {
$('#hazards').multiselect({
maxHeight: 300,
buttonWidth: '505px',
dropRight: true,
nonSelectedText: 'Select Hazard Statement(s)',
includeResetOption: true,
includeResetDivider: true,
enableFiltering: true,
includeFilterClearBtn: true,
enableCaseInsensitiveFiltering: true,
});
});
</script>
<!-- Initialize the plugin: -->
<script>
$(document).ready(function() {
$('#hp_codes').multiselect({
maxHeight: 300,
buttonWidth: '505px',
dropRight: true,
nonSelectedText: 'Select Hazard Properties',
includeResetOption: true,
includeResetDivider: true,
enableFiltering: true,
includeFilterClearBtn: true,
enableCaseInsensitiveFiltering: true,
});
});
</script>
and finally to put the mutliselect within the form
<div class="form-group">
<label>Hazard Statements: <span class="text-danger">*</span></label><br>
<!-- Build your select: -->
<select id="hazards" name="hazards[]" class="form-control" multiple="multiple" required>
<?php echo fill_hazards_select_box($con);?>
</select>
</div>
<div class="form-group">
<label>Hazard Properties: <span class="text-danger">*</span></label><br>
<!-- Build your select: -->
<select id="hp_codes" name="hp_codes[]" class="form-control" multiple="multiple" required>
<?php echo fill_hazards_select_box($con);?>
</select>
</div>