I have drop down menu that is populated with different US states. When the user selects different states, it sends this.value to an ajax function that populates another drop down menu filled with the different cities within that state.
The good news is that it works... for the most part. however when I select a state with 2 words such as "New York", it doesnt find anything. I recon its because there is a white space in the middle. therefore this.value is actually passing only "New" and not "New York". Below is my PHP code and AJAX function. can anyone tell me what i have missed here?! thanks a million :-)
//if result returns a value
if ($result != NULL){
$row = mysql_fetch_assoc($result);
$countryCode = $row['Code'];
if ($countryCode != NULL){
$sql = "SELECT DISTINCT District FROM City WHERE CountryCode = '$countryCode'";
$result = mysql_query($sql);
?>
<select name="state" onchange="getCity('<?=$country?>',this.value)">
<option>Select State</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['District']?>"><?=$row['District']?></option>
<? } ?>
</select>
<?php
}
}
function getCity(countryId, stateId) {
var strURL="findCity.php?country="+countryId+"&state="+stateId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}