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

Select a region based on a country selected

P: 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
Share this Question
Share on Google+
3 Replies


P: 11
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

FullyH3ktik
P: 52
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
Expert 5K+
P: 5,821
mso5 and ajcolburn:

Please use CODE tags when posting source code:

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

etc.
Oct 7 '07 #4

Post your reply

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