473,395 Members | 1,675 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,395 software developers and data experts.

Select a region based on a country selected

1
Hi,

I am trying to build a small form with a list of countries and regions by each country (yes, the classical one), and I'm stuck with it. I'm using AJAX also, and I manage to get the correct value from the selection, but it does not modify the second list based on the first selection. Can anyone tell me what am I doing wrong in this code? I am not a programmer..

Thank you!


Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. <!-- 
  3. //Browser Support Code
  4. function ajaxFunction(){
  5.     var ajaxRequest;  // The variable that makes Ajax possible!
  6.  
  7.     try{
  8.         // Opera 8.0+, Firefox, Safari
  9.         ajaxRequest = new XMLHttpRequest();
  10.     } catch (e){
  11.         // Internet Explorer Browsers
  12.         try{
  13.             ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  14.         } catch (e) {
  15.             try{
  16.                 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  17.             } catch (e){
  18.                 // Something went wrong
  19.                 alert("Your browser broke!");
  20.                 return false;
  21.             }
  22.         }
  23.     }
  24.     // Create a function that will receive data sent from the server
  25.     ajaxRequest.onreadystatechange = function(){
  26.         if(ajaxRequest.readyState == 4){
  27.             document.getElementById("selCount").innerHTML=ajaxRequest.responseText;
  28.         }
  29.     }
  30.  
  31.     var selCountry1 = document.getElementById("selCountry").value;
  32.     alert(selCountry1);
  33.     var queryString = "?selCountry=" + selCountry1;
  34.  
  35.  
  36. ajaxRequest.open("POST", "despre_dealeri_ajax1.php" + queryString, true);
  37. ajaxRequest.send(null); 
  38. }
  39. //-->
  40. </script>
Expand|Select|Wrap|Line Numbers
  1.  
  2.  <?php 
  3.  
  4. $db_countries = mysql_query("SELECT id, name FROM country") or die(mysql_error());
  5. $db_cities = mysql_query("SELECT DISTINCT id,name FROM region WHERE country_id = '".$_POST['selCount']."'") or die(mysql_error());
  6. //echo $_POST['selCountry'];
  7. $db=mysql_query("SELECT * FROM region WHERE id = \"".$_POST['selCity']."\"")or die(mysql_error());
  8. ?>
  9.  
  10.  
  11.  
  12. <div id="content"> 
  13.   <div id="text"> 
  14.     <h1><span>Dealeri</span></h1>
  15.     Cauta dealeri din: 
  16.     <form name="frmDealer" action="despre_dealeri_ajax1.php" method=post enctype="multipart/form-data" >
  17.       <INPUT type=hidden name="selCount">
  18.       <SELECT name="selCountry" onChange="ajaxFunction();"/>
  19.       <OPTION value="">SELECT ONE</OPTION>
  20.         <?php 
  21.  
  22.             while($re1=mysql_fetch_array($db_countries))
  23.             {
  24.  
  25.         ?>
  26.         <OPTION value="<?php echo $re1['id']; ?>" ><?php echo $re1['name']." ".$re1['id']; ?></OPTION>
  27.         <?php
  28.         }
  29.         ?>
  30.       </SELECT>
  31.       &nbsp;&nbsp;&nbsp;&nbsp;
  32.       <SELECT name="selCity"/>
  33.       <OPTION value="">SELECT ONE</OPTION>
  34.         <?php 
  35.             $i=0;
  36.             while($re11=mysql_fetch_array($db_cities))
  37.             {
  38.             $i++;
  39.         ?>
  40.         <OPTION value="<?php echo $re11['id']; ?>" <?php echo ($i==1?" DEFAULT":"");  ?>><?php echo $re11['name']; ?></OPTION>
  41.         <?php
  42.         }
  43.         ?>
  44.       </SELECT>
  45.       <!-- <INPUT type=submit value="&nbsp;Afiseaza&nbsp;" name=btnSubmit> -->
  46.     </form>
Oct 7 '07 #1
3 3026
At a glance, your javascript uses the getElementById method, but your form inputs do not have id's. Make sure that any elements used with this method have an id, for example:
Expand|Select|Wrap|Line Numbers
  1. <INPUT type=hidden name="selCount" id="selCount" />
  2. <SELECT name="selCountry" id="selCountry" onChange="ajaxFunction();"/>
Oct 7 '07 #2
or you could use getElementsByName, when you use the getElementsByName method, all elements in the document that have the specified name attribute or id attribute value are returned.
ajcolburn & mso5, please use code tags when posting code, it makes it a lot easier to read and resolve your problem
Oct 7 '07 #3
pbmods
5,821 Expert 4TB
mso5 and ajcolburn:

Please use CODE tags when posting source code:

[CODE=html]
HTML code goes here.
[/CODE]

etc.
Oct 7 '07 #4

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

Similar topics

4
by: point | last post by:
Hello there... I'm a PHP programmer and starting to learn JS... I have a following problem.... I have 3 select boxes! one is hotel one is destination and one is country... if someone...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
18
by: CJM | last post by:
I'm building a search function for one of my applications. The user has the option to enter a number criteria of criteria, but none are compulsary. I need to be able to build up a query string that...
1
by: jtwright | last post by:
I've got a view that creates a parent child relationship, this view is used in Analysis Services to create a dimension in a datastore. This query tends to deadlock after about 10 days of running...
0
by: Guillaume Houssay | last post by:
I need som ehelp concerning queries using SLEECT ... FOR UPDATE I have two tables (see description below) One is the table of reference, the other one is a table where i have daily infoirmation...
1
by: Mr. SweatyFinger | last post by:
I have a gridview and a sqlserver datasource. I also have a dropdownlist of countries. The gridview displays a bunch of crap about whatever country is selected in the countries dropdownlist I...
4
by: John | last post by:
Hi <form nam="country" ... <select name="industy" ... <option ... selected... </option> <option ... Now, in Javascript, I want to change defaulted "selected" to fourth option.
2
by: Sudhakar | last post by:
i have two select tags as part of a registration form, city1 city2 where city1 has a list of regions and similar for city2 there are different regions for city1 and city2 so instead of all the...
3
by: fishctr | last post by:
Hi There, I am creating a survey that asks users what countries they have worked in, and which regions in those countries. Because there can be many answers, I was thinking I could populate a...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.