The code below allows user selection of a drop-down id="division" to re-populate a drop-down id="branch_no". Notice the method jQuery.getJSON - that's making an AJAX call to a PHP page that does the query and returns JSON data.
$('#division').change(function() {
jQuery.getJSON( 'gAjaxJsonBranches.php?division=' + $('#division').val(), null, function(jdata) {
var jObj = jQuery.parseJSON(jdata);
var $el = $("#branch_no");
$el.empty();
$el.append($("<option></option>").attr("value", '').html('<<SELECT>>'));
$.each(jObj.searches, function(key, value) {
$el.append($("<option></option>").attr("value", value['id']).text(value['item']));
});
});
});
Code for gAjaxJsonBranches.php, uses a database lookup:
<?php
$conn = new PDO('mysql:host=yourhost;dbname=yourdb', 'user', 'password');
$search_list = '{ "searches": [ ';
$l_sql = "SELECT b.branch_no, b.branch_name " ;
$l_sql .= "FROM branches b INNER JOIN divisions d ON b.divisions_id = d.id " ;
$l_sql .= "WHERE d.division = '" . $_GET["division"] . "' " ;
$l_sql .= "ORDER BY b.divisions_id, b.branch_no ";
foreach ($conn->query($l_sql) as $l_row) {
if(trim($l_row[1]) != '') {
$search_list .= '{' ;
$search_list .= '"id": "' . $l_row[0] . '",' ;
$search_list .= '"item": "' . $l_row[0] . '-' . $l_row[1] . '"' ;
$search_list .= '},' ;
}
}
$search_list = substr($search_list, 0, -1);
$search_list .= '] }';
header('Content-type: text/plain');
echo json_encode($search_list);
exit();