/    Sign up×
Community /Pin to ProfileBookmark

AJAX only sees one PHP file

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[/B] fires my CT_ajax_Counties file, not my CT_ajax_PostMiles. The resulting third listbox is exactly the same as my second one. This doesn’t even makes sense, because I’m passing the parameter roadWay in the first one, but I’m passing the parameter countyString in the second function, so my SQL query shouldn’t return any values (that’s an important clue, but I still can’t figure it out).

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>
[/CODE]

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>”;
}
?>
[/CODE]

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);

}
[/CODE]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Arch_Stanton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...