I have two chained dropdown lists almost working. There is an issue with the second dropdown list remembering the choice saved in the db. Choosing an option from the first dropdown list filters the second. The first dropdown list choice and the second dropdown list choice are saved in the db. The first dropdown list retains the selection when the page is loaded/saved again, but the second does not. Below is my coding. Can anyone suggest a solution? I'm almost there, i just need the second dropdown list to return/display the selection saved in the db.
my ajax call:
Code: Select all
<script type="text/javascript">
$(document).ready(function() {
$("#sector").change(function() {
var sector=$("#sector").val();
$.ajax({
type:"post",
url:"getindustry.php",
data:"sector="+sector,
success:function(data) {
$("#industry").html(data);
}
});
});
});
</script>
Code: Select all
<?php
$sector = $_POST['sector'];
$query_sector = mysqli_query($dbc,"SELECT sector FROM sectors ORDER BY sector");
while($row_sector=mysqli_fetch_array($query_sector)) {
$sector_selected = ($row_sector['sector']==$sector) ? ' selected="selected"' : '';
$sector_options .= "<option value=\"{$row_sector['sector']}\"{$sector_selected}>{$row_sector['sector']}</option>\n";
}
?>
<select name="sector" id="sector" >
<option value="">< select a sector > <?php echo $sector_options ?></option>
</select>
<select name="industry" id="industry">
<option disabled>Select Industry</option>
</select>
Code: Select all
<?php
ob_start();
session_start();
include ('database_connection.php');
if(!isset($_SESSION['Email'])){
$page ="index.php";
header("Refresh: 0; url=$page");
echo " ";
}
$loggedin = $_SESSION['Email'];
$industry_sql = "SELECT industry FROM companies WHERE contact_email='$loggedin'";
$industry_result=mysqli_query($dbc,$industry_sql);
$industry_row=mysqli_fetch_assoc($industry_result);
if($industry_row) {$industry = $industry_row['industry'];}
?>
<option disabled>Select industry</option>
<?php
$sector = $_POST['sector'];
$industry_query = mysqli_query($dbc,"select industry from industries where sector ='$sector' order by sector");
while($row_industry=mysqli_fetch_array($industry_query)){
$industry_selected = ($industry_row['industry']==$industry) ? ' selected="selected"' : '';
echo "<option value=\"{$industry_row['industry']}\"{industry_selected}>{$industry_row['industry']}</option>\n";
}
?>