I have a select that is echoing out a column (jobTitle) from the database, on change I'm trying to display in another html select, selecting another column (company_name), but only values related to the first html select.
I'm not getting an errors. The first html select value is being passed, but for some reason it's not selecting from the db based on that in the second select.
I'm thinking the issue is with my second sql query.
P.S if anyone knows of a more effective way of doing this i'd be most gratefully to find out.
PHP & HTML:
<select name="jobtitle_select" class="jobtitle_select">
<option class="" name="" value="" >-- Job title --</option>
<?php
$sql = $dbh->prepare("SELECT * FROM jobs_list");
if($sql->execute()) {
$sql->setFetchMode(PDO::FETCH_ASSOC);
}
while($row = $sql->fetch()) {
$jobTitle = $row['jobTitle'];
echo "<option class='' name='' value='$jobTitle' > $jobTitle </option>";
} // end of while // ?>
</select>
<?php
$jobtitle_select = $_POST['jobtitle_select'];
if ($jobtitle_select){
$sql = $dbh->prepare("SELECT * FROM jobs_list WHERE company_name = :jobtitle_select");
$sql->bindParam(':jobtitle_select', $jobtitle_select, PDO::PARAM_STR);
if($sql->execute()) {
$sql->setFetchMode(PDO::FETCH_ASSOC);
}
?>
<select class="company_name_select" >
<option class="" name="" value="" >-- Company name --</option>
<?php while($row = $sql->fetch()) {
$company_name = $row['company_name'];
echo "<option class='' name='' value='$company_name'> $company_name </option>";
} // end of while //
}?> <!-- end of if -->
</select>
JQUERY:
$('.jobtitle_select').change(function(){
$.ajax({
//create an ajax request to load_page.php
type: "POST",
data:$('.jobtitle_select'),
dataType: "html", //expect html to be returned
success: function(date){
$('.company_name_select').html(date);
}
})
});