473,398 Members | 2,212 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,398 software developers and data experts.

Dynamic List Not Working

133 100+
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
13 1304
acoder
16,027 Expert Mod 8TB
Hi Adam, welcome to TSDN!

Check the responseXML object. Does it have the data you expect?
Jan 31 '08 #2
adamjblakey
133 100+
Thank you.

How would i go about checking this?
Jan 31 '08 #3
acoder
16,027 Expert Mod 8TB
Try alerting it.
Jan 31 '08 #4
adamjblakey
133 100+
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
16,027 Expert Mod 8TB
That should be document.forms[0].elements['region'].options.
Jan 31 '08 #6
adamjblakey
133 100+
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
16,027 Expert Mod 8TB
Is this the only or first form on the page?
Jan 31 '08 #8
adamjblakey
133 100+
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
16,027 Expert Mod 8TB
Instead of forms[0], use forms[formName] to target the particular form.
Jan 31 '08 #10
adamjblakey
133 100+
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
16,027 Expert Mod 8TB
That should be document.forms['register'].
Jan 31 '08 #12
adamjblakey
133 100+
Thanks a lot acoder it is working fine now.

Thanks a lot for sticking with me on this one :)
Jan 31 '08 #13
acoder
16,027 Expert Mod 8TB
No problem. Post again if you have more questions.
Jan 31 '08 #14

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

Similar topics

1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
3
by: Amp Inthalangsy | last post by:
Hi there, Was hoping someone would have an fix for me or some kind or work around. I was able to get my client side dynamic dependent list box working but if my child list data has double...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
1
by: npverni | last post by:
I have a fairly complex form that needs to load and maintain the state of several different dynamic user controls. Here is the workflow: 1) A series of editable user controls (each containing...
4
by: phcmi | last post by:
I have a PropertyGrid question. My task is to replace a legacy dialog box presentation with a modern one. The dialog itself allows the user to set configuration settings in our application, so...
0
by: mtsylvester | last post by:
Hi All, How do I add a List Box to a dynamic Details View. I want to replace the check boxes with list boxes. If I try to simply replace the check box with a list box it complains about not being...
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.