I am new to node.js and express. I am writing a node.js application with express and express-handlebars as my templating framework. I want to implement a dependant picklist which will be rendered based on the chosen value of another picklist.
So far this is what I have:
This is my parent picklist
<select id="buildingsSelect" class="building-select-add "name="residentBuildings">
{{#each buildingRows}}
<option value="{{idBuildings}}" name={{Name}}>{{Name}}</option>
{{/each}}
</select>
When the user selects a building, I make an ajax request,
$('.building-select-add').change(function(){
var buildingId = $('.building-select-add').val();
$.ajax({
type: 'post',
url: '/resident_add/' + buildingId,
success: function(data) {
location.reload();
},
error: function(err) {
console.log('------------ Error while updating this resident: ' + err);
console.log('------------ Error while updating this resident: ' + err.message);
}
});
});
and send the value which is used to make a database call to a Residents database.
router.post('/:buildingId', function(req, res){
var buildingId = req.params.buildingId;
console.log('OOOOOO buildingId: ' + buildingId);
var resList = [];
connection.query('select idResidents, Name from Residents where BuildingId = ?', buildingId, function(err, rows){
if (err) {
console.log('----------------------- ERROR: Error querying records - ' + err);
console.log('----------------------- ERROR: ' + err.message);
return err;
}
resList = rows;
});
});
I want the residents to be displayed in another select but I am not able to populate it.
How do I render the select on the page? If I do a location.reload() in ajax the entire page is reloaded. Is there a way to achieve this?