I have two select dropdowns on a php form submission page. The first dropdown is basic and shows/hides the second dropdown. The second dropdown is a category/filter list that is populated by a fetch pdo loop. I am trying to auto select the second dropdown(category) based on what the user selects in the first dropdown.
I have tried to populate the second based on the first dropdown, but for these you have to assign a value to be sorted and my dropdown's value (id) is auto populated by the database in a loop and I can't figure out how to get the single value (Echo out values in foreach loop) from each category or how to reference a php value in jquery. (populate second dropdown based on selected value of first dropdown)
Other research i've done: What's the best and easiest way to Populate a dropdown based on another dropdown https://www.daftlogic.com/information-programmatically-preselect-dropdown-using-javascript.htm
This is the last feature I need to develop for this project before I hand it in and I am just learning back-end development - I have been trying to figure this out for a week and have researched and implemented for hours. I would be really grateful to any guidance provided. I am not strong in jquery or javascript.
My database for the categories:
tbl_category
|------------------|
|cat_id | cat_name |
|------------------|
|1 | One |
|------------------|
|2 | Two |
|------------------|
|3 | Three |
|------------------|
|4 | Four |
|------------------|
|5 | Jeopardy |
|------------------|
|6 | Final |
|------------------|
my code (admin_addquestion.php):
<?php
require_once 'scripts/config.php';
confirm_logged_in();
$cat_tbl = 'tbl_category';
$product_categories = getAll($cat_tbl);
if (isset($_POST['submit'])) {
$cat = $_POST['catList'];
$result = addQuestion($cat);
$message = $result;
}
?>
<script>
$(function() {
$('.questionselector').change(function(){
$('.rounds').hide();
$('#' + $(this).val()).show();
});
});
</script>
<form action="admin_addquestion.php" method="post" enctype="multipart/form-data" class="md-form">
<Select class="questionselector">
<option value="none">Select a Question Type</option>
<option value="one">Round One</option>
<option value="two">Round Two</option>
<option value="three">Round Three</option>
<option value="four">Round Four</option>
<option value="five">Round Five</option>
<option value="six">Final Round</option>
</Select>
<select name="catList" id="catlist select" class="mdb-select md-form mb-4 initialized" >
<option value="" disabled selected>Insert into Category...</option>
<?php while ($product_category = $product_categories->fetch(PDO::FETCH_ASSOC)): ?>
<option value="<?php echo $product_category['cat_id']; ?>">
<?php echo $product_category['cat_name']; ?>
</option>
<?php endwhile; ?>
</select>
<div id="one" class="rounds" style="display:none">
content
</div>
<div id="two" class="rounds" style="display:none">
content
</div>
<div id="three" class="rounds" style="display:none">
content
</div>
<div id="five" class="rounds" style="display:none">
content
</div>
<div id="six" class="rounds" style="display:none">
content
</div>
<button class="btn btn-outline-info btn-rounded btn-block z-depth-0 my-4 waves-effect" type="submit" name="submit">Submit</button>
</form>
Currently, a user selects a round from the first dropdown (round one, round two, etc) and then they must independently select which category/round to insert the content/questions into from the second dropdown (One, Two, Three, Four, Five, Six, Final). The second dropdown's options are already populated by the database. I would like the user to be able to select a round, and have the second dropdown auto-selected based on their choice from the first select/dropdown. (eg, user selects Round One and then in the second dropdown, One is already chosen for them.)