Hi there, I found this great code that allows for chained selects with php and ajax. I have 2 boxes working great, and almost 3 - that is where I have a problem.
Here is the code:
var ajax = new Array();
function getAgencyList(sel)
{
var bargainingUnit = sel.options[sel.selectedIndex].value;
document.getElementById('agency').options.length = 0; // Empty agency select box
if(bargainingUnit.length>0){
var index = ajax.length;
ajax[index] = new sack();
ajax[index].requestFile = 'getAgency.php?bargainingUnit='+bargainingUnit; // Specifying which file to get
ajax[index].onCompletion = function(){ createAgency(index) }; // Specify function that will be executed after file has been found
ajax[index].runAJAX(); // Execute AJAX function
}
}
function createAgency(index)
{
var obj = document.getElementById('agency');
eval(ajax[index].response); // Executing the response from Ajax as Javascript code
}
function getFacilityList(sel)
{
var agency = sel.options[sel.selectedIndex].value;
document.getElementById('facility').options.length = 0; // Empty facility select box
if(agency.length>0){
var index = ajax.length;
ajax[index] = new sack();
ajax[index].requestFile = 'getFacility.php?agency='+agency; // Specifying which file to get
ajax[index].onCompletion = function(){ createFacility(index) }; // Specify function that will be executed after file has been found
ajax[index].runAJAX(); // Execute AJAX function
}
}
function createFacility(index)
{
var obj = document.getElementById('facility');
eval(ajax[index].response); // Executing the response from Ajax as Javascript code
}
</script>
So you can see that I just duplicated the Agency code block and made a Facility code block. When I go directly to the getFacility.php page, it is pulling the correct information, I just cannot get it to show in the checkbox... Here is the code I use for the forms:
<select id="bargainingUnit" name="bargainingUnit" onchange="getAgencyList(this)">
<option value="">Select a Bargaining Unit</option>
PHP CODE PULLING IN THE SELECTS </select>
</td>
</tr>
<tr>
<td>Agency: </td>
<td><select id="agency" name="agency" onchange="getFacilityList(this)">
</select>
</td>
</tr>
<tr>
<td>Facility: </td>
<td><select id="facility" name="facility">
</select>
</td>
</tr>
The Bargaining Unit pulls in fine, the associated Agencies show up fine, but the associated Facilities do not show in their select box. Although I do know that they are being correctly generated in the php page.
Here is a working example - http://www.powerflexweb.com/wtf.php
Can anyone see what I am missing? Thanks so much...