AJAX Question

Discussion in 'Programming' started by adamjblakey, May 21, 2008.

  1. #1
    Hi,

    I am currently using this to control a country and region drop down. So when i select a country the region box will fill with the results automatically.

    // JavaScript Document
    
    	var AdminResponse = "";
    
    	function parseResponse(){
    
    		var nText = AdminResponse.getElementsByTagName('optionText');
    		var nVal = AdminResponse.getElementsByTagName('optionVal');
    		document.forms[0]['regionid'].options.length = 1;
    		for (i=0; i<nText.length; i++)
    			{ 
    			 var nOption = document.createElement('option'); 
    			 var isText = document.createTextNode(nText[i].firstChild.data); 
    			 nOption.setAttribute('value',nVal[i].firstChild.data); 
    			 nOption.appendChild(isText); 
    			 document.forms[0]['regionid'].appendChild(nOption); 
    			}
    	}
    
    	function update(nVal){
    
    		var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
    		AdminRequest.onreadystatechange = function()
    			{
    		 	 if (AdminRequest.readyState == 4)
    				{
    		 	 	 if (AdminRequest.status == 200)
    					{
    			 	 	 AdminResponse = AdminRequest.responseXML;
    			 	 	 parseResponse();
    					}
    		 	 	 else 	{
    				 	 alert('Error Update.php File '+ AdminRequest.statusText);
    					}
    				}
    			}
    		var infoStr = "?choice="+nVal;
    		AdminRequest.open("GET", "Update.php"+infoStr, true);
    		AdminRequest.send(null); 
    	}
    Code (markup):
    <?php 
    
    	$choice = $_GET['choice'];
    	$xml = "<?xml version='1.0' ?><options>";
    
    	include ("../config/db.config.php");
    
    	$query = "SELECT * FROM regions WHERE countryid = '$choice'";
    	$result = @mysql_query($query);
    	$num = @mysql_num_rows($result);
    	if ($result && $num > 0)
    		{
    	 	 while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
    			{
    	 	 	 $xml .= "<optionText>" . $row['region'] . "</optionText><optionVal>" . $row['id'] . "</optionVal>";
    			}
    		}
            $xml .= "</options>";
    	@mysql_free_result($result);
    	@mysql_close();	
    	header("Content-Type: text/xml");
    	echo $xml;
    
    ?>
    PHP:
    This works fine but now i want to include a third box for cities so when a region is selected the the cities dropdown will automatically fill.

    How can i do it with the current code?

    Cheers,
    Adam
     
    adamjblakey, May 21, 2008 IP