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

Dynamic List Not Working

100+
P: 133
Hi,

I have a dynamic country and region list i am trying to get working but for some reason when i select a country the next region field does not populate.

Here is the code:

HTML
Expand|Select|Wrap|Line Numbers
  1. <select name="country" onchange="update(this.value)">                        <option value="184">USA</option>
  2.                                                 <option value="183">United Kingdom</option>
  3.                 </select>
  4.  
  5.                 <select name="region" onchange="alert(this.value)">
  6.                     <option value="">Make a selection </option>
  7.                 </select>
  8.  
  9.  

Javascript
Expand|Select|Wrap|Line Numbers
  1.  
  2.     var AdminResponse = "";
  3.  
  4.     function parseResponse(){
  5.  
  6.         var nText = AdminResponse.getElementsByTagName('optionText');
  7.         var nVal = AdminResponse.getElementsByTagName('optionVal');
  8.         document.forms[0]['region'].options.length = 1;
  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[0]['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.  
  41.  

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]


SQL Structure

id / countryid / region

1 / 184 / Brighton
2 / 184 / Berkshire
3/ 184 / Bath Avon
4 / 184 / Bedfordshire
5 / 183 / Alabama
6 / 183 / Alaska
7 / 183 / American Samoa
8 / 183 / Arizona


Any help would be greatfully appreciated.

Cheers,
Adam
Jan 31 '08 #1
Share this Question
Share on Google+
13 Replies


acoder
Expert Mod 15k+
P: 16,027
Hi Adam, welcome to TSDN!

Check the responseXML object. Does it have the data you expect?
Jan 31 '08 #2

100+
P: 133
Thank you.

How would i go about checking this?
Jan 31 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Try alerting it.
Jan 31 '08 #4

100+
P: 133
I have just ran Firebug on the page when i select a country and it shows me the following error:

document.forms[0].region has no properties
[Break on this error] document.forms[0]['region'].options.length = 1;

Any Ideas?
Cheers,
Adam
Jan 31 '08 #5

acoder
Expert Mod 15k+
P: 16,027
That should be document.forms[0].elements['region'].options.
Jan 31 '08 #6

100+
P: 133
Hi,

Now i am getting the following error:

document.forms[0].elements.region has no properties
[Break on this error] document.forms[0].elements['region'].options.length = 1;

Cheers,
Adam
Jan 31 '08 #7

acoder
Expert Mod 15k+
P: 16,027
Is this the only or first form on the page?
Jan 31 '08 #8

100+
P: 133
Yes there is 2 other forms on the page. I have just removed these and it works fine.

How can i get this to work with the other forms?
Jan 31 '08 #9

acoder
Expert Mod 15k+
P: 16,027
Instead of forms[0], use forms[formName] to target the particular form.
Jan 31 '08 #10

100+
P: 133
Thank you for that,

I have tried document.form.name['register'].elements['region'].options.length = 1; but does not seem to be working. Have i done something wrong?
Jan 31 '08 #11

acoder
Expert Mod 15k+
P: 16,027
That should be document.forms['register'].
Jan 31 '08 #12

100+
P: 133
Thanks a lot acoder it is working fine now.

Thanks a lot for sticking with me on this one :)
Jan 31 '08 #13

acoder
Expert Mod 15k+
P: 16,027
No problem. Post again if you have more questions.
Jan 31 '08 #14

Post your reply

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