Hello All,

I'm working on a script which I'm sure has been done many times, using 3 dropdown list boex to select country, state, and city.
Using PHP, AJAX, and MYSQL

Whats working:
I'm able to populate the country and select. At the sametime I can populate the the state based on country select.

Whats not working:
city selection.
- not getting any errors to work with either.
the state variable is passed throubh but the country isnt.
I believe the problem is in "function getCity(countryId,stateId)" ajax script

Any help you can give me would be greatly appreciated.

<html>
<head>
<title> City wide Helpdesk </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript"> 

function getXMLHTTP() 
	{ //fuction to return the xml http object
		var xmlhttp=false;	
		try{ xmlhttp=new XMLHttpRequest(); }
		catch(e) {	
			try{ xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");}
				catch(e){
				try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
					catch(e1){ xmlhttp=false;	}
				}
			}
		return xmlhttp;
    }

function getState(countryId) 
{	var strURL="findprovstate.php?country="+countryId;
	var req = getXMLHTTP();
	if (req) 
	{	req.onreadystatechange = function() 
		{	if (req.readyState == 4) 
			{	if (req.status == 200) 
				{	document.getElementById("statediv").innerHTML=req.responseText;	}
				else 
				{   alert("There was a problem while using XMLHTTP:\n" + req.statusText);	}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}		
}

function getCity(countryId,stateId) 
{	var strURL="findCity.php?country="+countryId+"&state="+stateId;
	var req = getXMLHTTP();
	if (req) 
	{	req.onreadystatechange = function() 
		{	if (req.readyState == 4) 
			{	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);
	}
}

</script>
</head>
<body>
<form method="post" name="form1">
 <table border="0" cellpadding="0" cellspacing="0" width="60%"><tbody>

  <tr>
   <td width="150">Country</td>
   <td width="150">
    <select style="background-color: #ffffa0" name="country" onchange="getState(this.value)">';
		<?php
			require_once("C:\wamp\www\servicebid\inc\FindCountry.php");
			while($row=mysqli_fetch_assoc($result)) 
			{if($nextcountry = $row['country'])
				{ echo '<option value="' . $row['country'] . '">' . $row['country'] . '</option>'; }
			}
			?>
	</select></td>
  </tr> 


 <tr style="">
    <td>State</td>
    <td >
	   <div id="statediv">
	       <select name="state" >
	            <option>Select Country First</option>
           </select>
	   </div>
	</td>
  </tr>

<tr style="">
	<td>City</td>
	<td ><div id="citydiv">
		<select name="city">
			<option>Select State First</option>
    	</select>
		</div>
	</td>
</tr>

</tbody>
</table>
</form>
</body>
</html>

This code is used to populate the state/province.

<?php
require_once("C:\wamp\www\servicebid\inc\connection.php");
$country = $_GET['country'];
$provinceState_query = "SELECT DISTINCT province from tb_location where country = '$country'";
$result= mysqli_query($dbconnect, $provinceState_query) or die('<br/>Error reading Database:'.mysql_error());
?>

<select name="state" onchange='getCity("<?=$country?>",this.value)'>
<option>Select State</option>
<?php
	while($row=mysqli_fetch_assoc($result)) 
		{ echo '<option value="' . $row['province'] . '">' . $row['province'] . '</option>'; }
?>
</select>

This code below is used to populate the city dropdown list box.
at this stage the state is comming through but tnot the country.

<?php
require_once("C:\wamp\www\servicebid\inc\connection.php");
$countryId = $_GET['country'];
$stateId = $_GET['state']; // works

echo '<br> ==>:1'.$countryId;
echo '<br> ==>:2'.$stateId;	//works
$city_query = "SELECT city from tb_location WHERE country ='$countryId' AND province='$stateId'";
$result= mysqli_query($dbconnect, $city_query) or die('<br/>Error reading Database:'.mysql_error());

while($row=mysqli_fetch_array($result)) 
{  //testing
	echo 'br>'.$row['city'];
}
?>
<select name="city">
<option>Select City</option>
<?php 
while($row=mysql_fetch_array($result)) 
{	echo '<option value="' . $row['city'] . '">' . $row['city'] . '</option>'; }
?>
</select>

    You need to isolate whether your country value is getting dropped by your javascript or by your PHP code. I would recommend putting an alert in your getCity function to say "country is X" and output the country value it receives. if it is not getting a valid country, then you need to look at the code that calls this function and see what the problem might be there.

    if that function is getting a valid country value, then try visiting findCity.php with the appropriate values and see if that script returns an error.

      Hello,
      Took the suggestions and added a tone of alerts and I believe the problem is sitll in the same place.
      the middle block of code and the java script is where the problem is but i thing its more or lest the middle block of code.
      I tested and tested over and over again and basically the country isnt going through. the country variable does exist.. i put alot of echo statement to confirm this.

      Not sure the format is correct... any ideas??

      select name="state"[B][COLOR="Red"] onchange="getCity('<?=$country ?>',this.value)"[/COLOR][/B]><option>Select State</option>

      JS:
      the state comes through but not the country...im lost.

      function getCity(country,stateId) 
      {	[COLOR="Red"][B]alert('Country==>:' + country + ' State==>: ' + stateId + '.');	[/B][/COLOR]
      var strURL="findCity.php?country="+country+"&state="+stateId;	var req = getXMLHTTP();	if (req) 	{	req.onreadystatechange = function() 		{	if (req.readyState == 4) 			{	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);	}}
      

        So I'm guessing your alert says:

        Country==>: State==>: CA.

        or something like that.

        Not sure (because you haven't explained much more than last time) but I'm guessing this code should be checked:

        <?php
        require_once("C:\wamp\www\servicebid\inc\connection.php");
        $country = $_GET['country'];
        $provinceState_query = "SELECT DISTINCT province from tb_location where country = '$country'";
        $result= mysqli_query($dbconnect, $provinceState_query) or die('<br/>Error reading Database:'.mysql_error());
        ?>
        
        <select name="state" onchange='getCity("<?=$country?>",this.value)'>
        

        If you view the source of your <select> object, is the country value empty? If so, this is probably because you haven't supplied a country value in $GET['country']. Not sure if you know this, but everything in $GET comes from values appended to your url:

        http://yourdomain.com/some_page.php?country=USA

          Hello All,

          Just in case someone out there was looking to answer this post I finally was able to solve the problem..

          Problem area:
          select name="state" onchange="getCity('<?=$country ?>',this.value)"><option>Select State</option>

          The $country wasn't getting the country variable so after looking at all the suggestions i decided to re write the php script located in the above script.

          onchange="getCity('<?php echo $country; ?>',this.value)"

          this seem to ffixed the problem.

            Write a Reply...