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
- var AdminResponse = "";
- function parseResponse(){
- var nText = AdminResponse.getElementsByTagName('optionText');
- var nVal = AdminResponse.getElementsByTagName('optionVal');
- document.forms["testForm"].elements['region'].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["testForm"]['region'].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);
- }
[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