473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

AJAX - Menu - Country, Region & City

133 100+
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
1 2398
acoder
16,027 Expert Mod 8TB
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

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

Similar topics

4
by: Trip | last post by:
Please if someone can help me !!! I need client and server code(principle based on AJAX) for next problem: i have 3 <select> tags on html page.(it must be NO page reload(callback) only...
8
by: DartmanX | last post by:
Hi, Looking for recommendations for a decent API for AJAX work. I need it to be somewhat documented so I can figure out how to actually use it. My most critical need right now is clean code...
0
by: Tim Marshall | last post by:
HI folks, I'm doing an app that requires the user to be able to choose a country, then a state/province/territory, then a city and possibly somewhere in-between, some kind of region, perhaps...
5
by: Mukesh | last post by:
Hi i want to use AJAX.net in my Existing Application I have already installed the ajax .net ..net 3.0 and using VS 2005 in the old application i have added a new web form then script manager...
3
by: Mukesh | last post by:
HI all I have created a user ctrl for user info. On which i have used some asp ctrls, .net Ajax (Atlas) Ctrls and validation as described below Name TxtBox1(RequiedFieldValidator,...
5
by: panos100m | last post by:
Hi there our developer is creating a website in Ajax. In one page we have the following fields. Country: City/State: International Code:
10
by: bhappy | last post by:
Hai All, im facing different problem with ajax. Im using ajax code for country,state & city dropdownlists. In city dropdownlist i had an option to add new city.Im selecting other option in city...
13
by: adamjblakey | last post by:
Hi, I am using the following function to do a country/region ajax drop down list. // JavaScript Document var AdminResponse = ""; function parseResponse(){
17
mikek12004
by: mikek12004 | last post by:
I have a search form with the first two fields being country and city. When the user selects a country I want the city select box to display only the cities of that country (I have already implement...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.