By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

AJAX - Menu - Country, Region & City

100+
P: 133
Hi,

At the moment i have a country and region ajax list so depending on which country you select the region field will auto fill.

Now this is the code that makes this work:

JS
Expand|Select|Wrap|Line Numbers
  1. var AdminResponse = "";
  2.  
  3.     function parseResponse(){
  4.  
  5.         var nText = AdminResponse.getElementsByTagName('optionText');
  6.         var nVal = AdminResponse.getElementsByTagName('optionVal');
  7.         document.forms["testForm"].elements['region'].options.length = 1;
  8.  
  9.         for (i=0; i<nText.length; i++)
  10.             { 
  11.              var nOption = document.createElement('option'); 
  12.              var isText = document.createTextNode(nText[i].firstChild.data); 
  13.              nOption.setAttribute('value',nVal[i].firstChild.data); 
  14.              nOption.appendChild(isText); 
  15.              document.forms["testForm"]['region'].appendChild(nOption); 
  16.             }
  17.     }
  18.  
  19.     function update(nVal){
  20.  
  21.         var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
  22.         AdminRequest.onreadystatechange = function()
  23.             {
  24.               if (AdminRequest.readyState == 4)
  25.                 {
  26.                    if (AdminRequest.status == 200)
  27.                     {
  28.                        AdminResponse = AdminRequest.responseXML;
  29.                        parseResponse();
  30.                     }
  31.                    else     {
  32.                       alert('Error Update.php File '+ AdminRequest.statusText);
  33.                     }
  34.                 }
  35.             }
  36.         var infoStr = "?choice="+nVal;
  37.         AdminRequest.open("GET", "Update.php"+infoStr, true);
  38.         AdminRequest.send(null); 
  39.     }
  40.  
PHP
[PHP]<?php

$choice = $_GET['choice'];
$xml = "<?xml version='1.0' ?><options>";

require_once('init.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['region'] . "</optionVal>";
}
}
$xml .= "</options>";
@mysql_free_result($result);
@mysql_close();
header("Content-Type: text/xml");
echo $xml;

?>[/PHP]

What i want to do is include a city field under the region so depending on which region i select the city field auto fills. I have already got a tables with the cities in called cities. How would i link this up with the existing code as i see it i will have problems because in the php it is checking for region not city.

Cheers,
Adam
Apr 10 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
Either call a different script for the cities, or use an extra parameter to the same script which would replace regions with cities depending on what was passed rather like the JavaScript should be changed to allow you to reuse the same function for cities.
Apr 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.