I have multiple HTML dropdowns. After one selection, I want it to automatically populate the next dropdown. All of this information is being brought in to populate the lists from a database using a SQL statement and foreach loop, so I cannot hard code the values like all of the examples out there related to my question. I currently have just a bit of JavaScript for this as of now although I am not sure if I am going in the right direction. I am thinking that this will need to involve some AJAX and an onChange listener. I am just unsure of how to get started.
So how can I do this? I am not asking for you to do this for me, but just some code (like an outline) to give me a head start and get me going would be appreciated! Thank you!
SQL Statements:
<?php
$host="xxxxxxx";
$dbName="xxxx";
$dbUser="xxxxxxxxxxxxxx";
$dbPass="xxxxxxxxxxx";
$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql_major = "SELECT DISTINCT [Major Category] FROM vProducts ORDER BY [Major Category] ASC";
$sql_minor = "SELECT DISTINCT [Minor Category] FROM vProducts ORDER BY [Minor Category] ASC";
$sql_code = "SELECT DISTINCT [Product Report Code] FROM vProducts ORDER BY [Product Report Code] ASC";
$dropdown_major = $dbh->query($sql_major);
$dropdown_minor = $dbh->query($sql_minor);
$dropdown_code = $dbh->query($sql_code);
?>
Dropdowns:
<table cellspacing="5" align="center" id="dropdown-table">
<thead>
<tr>
<th>Major Category</th>
<th>Minor Category</th>
<th>Report Code</th>
<th>SKU</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select id="major" onChange="updateCat();">
<option value="" disabled="disabled" selected="selected">Please Select One</option>
<?php foreach ($dropdown_major->fetchAll() as $drop_major): ?>
<option
value=""
data-name="<?php echo $drop_major ['Major Category'];?>"
>
<?php echo $drop_major ['Major Category'];?>
</option>
<?php endforeach; ?>
</select>
</td>
<td>
<select id="minor">
<option value="" disabled="disabled" selected="selected">Please Select One</option>
<?php foreach ($dropdown_minor->fetchAll() as $drop_minor): ?>
<option
value=""
data-name="<?php echo $drop_minor ['Minor Category'];?>"
>
<?php echo $drop_minor ['Minor Category'];?>
</option>
<?php endforeach; ?>
</select>
</td>
<td>
<select>
<option value="" disabled="disabled" selected="selected">Please Select One</option>
<?php foreach ($dropdown_code->fetchAll() as $drop_code): ?>
<option
value="code"
data-name="<?php echo $drop_code ['Product Report Code'];?>"
>
<?php echo $drop_code ['Product Report Code'];?>
</option>
<?php endforeach; ?>
</select>
</td>
<td>
<select>
<option value="" disabled="disabled" selected="selected">Please Select One</option>
<option value="sku">SKU</option>
</select>
</td>
<td><input type="button" value="Search" id="searchButton" onclick="show();"></td>
<td><button class="create-user" id="insertButton">Add Group</button></td>
</tr>
</tbody>
</table>
JavaScript:
// JS for Dropdown
function updateCat() {
var e = document.getElementById("major");
var majorSelected = e.options[e.selectedIndex];
document.getElementById("minor").value = majorSelected.dataset.name;
}