I have a page with an HTML Select object (listbox). When my user makes a selection, I’m using AJAX to create and populate a second listbox. Works great. Then I want my user to select options in the second listbox and use AJAX to fill a third listbox. That’s my problem.
But both of my AJAX functions fire the first PHP script, not the second. In the code below, my FillCountyBox function fires the code in my CT_ajax_Counties file, giving me the results I want. But my FillPostMileBox [B]also
Sorry for posting a ton of code – I usually try to post only the snippet that’s giving me trouble, but I can’t figure out where my problem is. As always, thanks much for any help.
Code for main page:
[CODE]
<tr>
<td class=”RCPSpacerDef”>
</td>
<td colspan=”2″ class=”RCPListboxDef”>
<?php
// query database for all roadways
$result = mysql_query(“SELECT DISTINCT sg.Roadway
FROM station_geographic as sg
WHERE LENGTH(sg.Roadway) > 0
ORDER BY sg.Roadway”) or die(mysql_error());
// create listbox
echo “<SELECT name=’RCP_Roadway’ id=’RCP_Roadway’
SINGLE SIZE=’12’ class=’MediumListbox’
onclick=”FillCountyBox(this.id);” >”;
while($row=mysql_fetch_array($result)) {
echo “<option value='”.$row[‘Roadway’].”‘>”.$row[‘Roadway’].”</option>”;
}
echo “</SELECT>”;
?>
</td>
<td colspan=”2″ class=”RCPListboxDef”>
<div id=”divCounties”></div>
</td>
<td colspan=”2″ class=”RCPListboxDef”>
<div id=”divPostMiles”></div>
</td>
<td class=”RCPSpacerDef”>
</td>
</tr>
PHP code that returns the second and third listbox objects:
[CODE]
<?php
if ($_GET[‘countyString’]) {
$result = mysql_query(“SELECT DISTINCT lp.county, sg.roadway
FROM location_political as lp
INNER JOIN location as l
INNER JOIN station_in_project as sip
INNER JOIN station as s
INNER JOIN station_geographic as sg
ON lp.locationpoliticalid = l.locationpoliticalid
AND l.locationid = sip.locationid
AND sip.stationid = s.stationid
AND s.stationgeographicid = sg.stationgeographicid
WHERE sg.postmile IN (“.$_GET[‘countyString’].”)
ORDER BY lp.county;”) or die(mysql_error);
echo “<SELECT name=’RCP_Postmiles[]’ id=’RCP_Postmiles[]’ MULTIPLE SIZE=’12’ class=’MediumListbox’>”;
while($row=mysql_fetch_array($result)) {
echo “<option value='”.$row[‘County’].” – “.$row[‘Postmile’].”‘>”.$row[‘County’].” – “.$row[‘Postmile’].”</option>”;
}
echo “</SELECT>”;
}
?>
<?php
if ($_GET[‘roadWay’]) {
$result = mysql_query(“SELECT DISTINCT lp.County
FROM location_political as lp
INNER JOIN location as l
INNER JOIN station_in_project as sip
INNER JOIN station as s
INNER JOIN station_geographic as sg
ON lp.locationpoliticalid = l.locationpoliticalid
AND l.locationid = sip.locationid
AND sip.stationid = s.stationid
AND s.stationgeographicid = sg.stationgeographicid
WHERE sg.roadway = ‘”.$_GET[‘roadWay’].”‘;”) or die(mysql_error);
echo “<SELECT name=’RCP_Counties[]’ id=’RCP_Counties[]’
MULTIPLE SIZE=’12’
class=’MediumListbox’
onclick=”FillPostMileBox(this.id)”>”;
while($row=mysql_fetch_array($result)) {
echo “<option value='”.$row[‘County’].”‘>”.$row[‘County’].”</option>”;
}
echo “</SELECT>”;
}
?>
AJAX functions:
[CODE]
function GetCounties() {
if (xmlHttpRequest.readyState == 4) {
document.getElementById(‘divCounties’).innerHTML = xmlHttpRequest.responseText;
}
}
function FillCountyBox(selectedControl) {
// get selected roadway, fill Counties list box with appropriate counties
var select_list_field = document.getElementById(selectedControl);
var select_list_selected_index = select_list_field.selectedIndex;
var roadWay = select_list_field.options[select_list_selected_index].text
//create XMLHttpRequest object
xmlHttpRequest =
(window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject(“Microsoft.XMLHTTP”);
//If the browser doesn’t support Ajax, exit
if (xmlHttpRequest == null) {
return;
}
//Initiate the XMLHttpRequest object
xmlHttpRequest.open(“GET”, “CT_ajax_Counties.php?roadWay=” + roadWay, true);
//Setup the callback function
xmlHttpRequest.onreadystatechange = GetCounties;
//Send the Ajax request to the server
xmlHttpRequest.send(null);
}
function GetPostMiles() {
// fill post miles list box with selected post miles
if (xmlHttpRequest.readyState == 4) {
document.getElementById(‘divPostMiles’).innerHTML = xmlHttpRequest.responseText;
}
}
function FillPostMileBox(selectedControl) {
// get selected counties, fill post mile list box with appropriate entries
var xmlHttpRequest, strCounties, i;
xmlHttpRequest.abort();
var listBox = document.getElementById(selectedControl);
strCounties = “”;
// compose a comma-delimited string of all selected counties
for (i =0; i < listBox.options.length; ++i) {
if (listBox.options[i].selected === true) {
strCounties = strCounties + listBox.options[i].value + “,”;
}
}
//create XMLHttpRequest object
xmlHttpRequest =
(window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject(“Microsoft.XMLHTTP”);
//If the browser doesn’t support Ajax, exit
if (xmlHttpRequest == null) {
return;
}
//Initiate the xmlHttpRequest object
xmlHttpRequest.open(“GET”, “CT_ajax_PostMiles.php?countyString=” + strCounties, true);
//Setup the callback function
xmlHttpRequest.onreadystatechange = GetPostMiles;
//Send the Ajax request to the server
xmlHttpRequest.send(null);
}