I found code for a javascript cascading dropdown menus. They seem to work fine for me unless I have a situation where one of the boxes only has one result.

Scenario 1:

The first dropdown is food and the second is fruits, vegetables and the third box has bananas, apples, turnip. In this situation it works fine.

Scenario 2:

The first dropdown is food and the second is fruits and the third box has bananas, apples. In this situation the third box will show bananas and not let me select apples.

The code is below. Any help would be greatly appreciated:

Code:
<SCRIPT type="text/javascript">
//Applies cascading behavior for the specified dropdowns
function applyCascadingDropdown(sourceId, targetId) {
    var source = document.getElementById(sourceId);
    var target = document.getElementById(targetId);
    if (source && target) {
        source.onchange = function() {
            displayOptionItemsByClass(target, source.value);
        }
        displayOptionItemsByClass(target, source.value);
    }
}

//Displays a subset of a dropdown's options
function displayOptionItemsByClass(selectElement, className) {
    if (!selectElement.backup) {
        selectElement.backup = selectElement.cloneNode(true);
    }
    var options = selectElement.getElementsByTagName("option");
    for(var i=0, length=options.length; i<length; i++) {
        selectElement.removeChild(options[0]);
    }
    var options = selectElement.backup.getElementsByTagName("option");
    for(var i=0, length=options.length; i<length; i++) {
        if (options[i].className==className)
            selectElement.appendChild(options[i].cloneNode(true));
    }
}

//Binds dropdowns
function applyCascadingDropdowns() {
    applyCascadingDropdown("CompanyId", "DivisionId");
    applyCascadingDropdown("DivisionId", "LocationId");
    applyCascadingDropdown("LocationId", "DepartmentId");
    //We could even bind items to another dropdown
    //applyCascadingDropdown("items", "foo");
}

//execute when the page is ready
window.onload=applyCascadingDropdowns;
</SCRIPT>
Here is the php and the code that displays the form.

Code:
Company:<br />
<select name="CompanyId" id="CompanyId">
<?php while ($row_company = mysql_fetch_array($company)) { ?>
    <option value="<?php echo $row_company['CompanyId']; ?>"><?php echo $row_company['Name']; ?></option>
<?php } ?>
</select><br />

Division:<br />
<select name="DivisionId" id="DivisionId">
	<?php while ($row_division = mysql_fetch_array($division)) { ?>
	    <option class="<?php echo $row_division['CompanyId']; ?>" value="<?php echo $row_division['DivisionId']; ?>"><?php echo $row_division['Name']; ?></option>
	<?php } ?>
</select><br />

Location:<br />
<select name="LocationId" id="LocationId">
	<?php while ($row_location = mysql_fetch_array($location)) { ?>
	    <option class="<?php echo $row_location['DivisionId']; ?>" value="<?php echo $row_location['LocationId']; ?>"><?php echo $row_location['Name']; ?></option>
	<?php } ?>
</select><br />

Department:<br />
<select name="DepartmentId" id="DepartmentId">
	<?php while ($row_department = mysql_fetch_array($department)) { ?>
	    <option class="<?php echo $row_department['LocationId']; ?>" value="<?php echo $row_department['DepartmentId']; ?>"><?php echo $row_department['Name']; ?></option>
	<?php } ?>
</select><br /><br />
<input type="submit" value="Submit">