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

Update second dropdown via ajax

100+
P: 250
Now I' using ajax. But I have two problems.
1. When I select a value from the drop down box I have got another drop down box same as earlier.
2.The selected value changed. I want to keep the selcted value.
This is my code
test.php
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $type=$_GET['Vtype'];
  3. echo $type;
  4. ?>
  5. <div id='txtHint'></div>
  6.  
  7.  
  8. <td>Make :</td> 
  9. <td>
  10.  
  11. <select name="Make"  class="normalTxt" onChange='showSelected(this.value); ' id="Make" >
  12. <!--<option value="Select" >Select</option>-->
  13. <?php    $mysql_result1 = mysql_query("SELECT DISTINCT vehicleMake FROM  vehicles");
  14. while($row = mysql_fetch_array($mysql_result1)){
  15. foreach( $row AS $key => $val )
  16. $Make = stripslashes( $val );
  17.  
  18. ?>
  19. <option value=<?php  echo  "'$Make'" ?> ><?php echo "$Make&nbsp;&nbsp;&nbsp;"?>
  20. <?php 
  21. }    
  22. ?>
  23. </option>
  24. </select>
  25. <?php
  26.  
  27. ?>
  28. </td>
  29.  
This is my ajax code
Expand|Select|Wrap|Line Numbers
  1. //====================Create Ajax object================================
  2. function GetXmlHttpObject() {
  3.     var request;
  4.     try {
  5.         request=new XMLHttpRequest();
  6.     }
  7.     catch (e) {
  8.         try {
  9.             request=new ActiveXObject("Msxml2.XMLHTTP");
  10.         }
  11.           catch (e) {
  12.             try {
  13.                 request=new ActiveXObject("Microsoft.XMLHTTP");
  14.             }
  15.             catch (e) {
  16.                 alert("Your browser does not support AJAX!");
  17.                 return false;
  18.             }
  19.         }
  20.     } 
  21.     return request;
  22. }
  23.  
  24. function showSelected(){
  25.  
  26. var dropdownIndex = document.getElementById('Make').selectedIndex;
  27. var ttype = document.getElementById('Make'[dropdownIndex].value;
  28. var request = GetXmlHttpObject();
  29. var url="test.php";
  30. url=url+"?Vtype="+ttype+1;
  31. url=url+"&sid="+Math.random();
  32. request.onreadystatechange=stateChanged;
  33. request.open("GET",url,true);
  34. request.send(null);
  35. function stateChanged()
  36.   {
  37.  if (request.readyState==4)  
  38.     {                        document.getElementById("txtHint").innerHTML=request.responseText;
  39.  
  40.     }
  41.  }
  42.  
  43. }
  44.  
Please help me...
Jul 20 '11 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,366
You declared your state change function inside another function.
Jul 20 '11 #2

100+
P: 250
But when I send values to another page. and display data; There is no problem.I think
Expand|Select|Wrap|Line Numbers
  1. <div id='txtHint'></div>
is the problem. But I don't know how to fix it. Please someone help me..
Jul 26 '11 #3

Rabbit
Expert Mod 10K+
P: 12,366
That's not the problem. The problem is that you set the onReadyState property to the function stateChanged. But then you tried to define that function inside the function. Either you set the onReadyState property to a function() {} or you set it to a function reference but then you define that function outside of the function that you're in.
Jul 26 '11 #4

Post your reply

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