AJAX & PHP drop down menu problem

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jun 2008
Posts: 23
Reputation: forwardlookguy is an unknown quantity at this point 
Solved Threads: 0
forwardlookguy forwardlookguy is offline Offline
Newbie Poster

AJAX & PHP drop down menu problem

 
0
  #1
Oct 24th, 2008
Hello, I got the following code from someone and have tailored it to fit my needs but I have a problem. The first menu populates from my MySQL database. However, when the first menu is selected, the second menu will not populate with the correct information. Zero is the only number that comes up. I think I have all the queries correct but I'll let everyone take a look at it.

Here's the main page:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <html>
  3. <head>
  4. <title>Roshan's Triple Ajax dropdown code</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <script language="javascript" type="text/javascript">
  7. // Roshan's Ajax dropdown code with php
  8. // This notice must stay intact for legal use
  9. // Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
  10. // If you have any problem contact me at http://roshanbh.com.np
  11. function getXMLHTTP() { //fuction to return the xml http object
  12. var xmlhttp=false;
  13. try{
  14. xmlhttp=new XMLHttpRequest();
  15. }
  16. catch(e) {
  17. try{
  18. xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
  19. }
  20. catch(e){
  21. try{
  22. xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  23. }
  24. catch(e1){
  25. xmlhttp=false;
  26. }
  27. }
  28. }
  29.  
  30. return xmlhttp;
  31. }
  32.  
  33. function getYear(makeid) {
  34.  
  35. var strURL="findState.php?make="+makeid;
  36. var req = getXMLHTTP();
  37.  
  38. if (req) {
  39.  
  40. req.onreadystatechange = function() {
  41. if (req.readyState == 4) {
  42. // only if "OK"
  43. if (req.status == 200) {
  44. document.getElementById('statediv').innerHTML=req.responseText;
  45. } else {
  46. alert("There was a problem while using XMLHTTP:\n" + req.statusText);
  47. }
  48. }
  49. }
  50. req.open("GET", strURL, true);
  51. req.send(null);
  52. }
  53. }
  54. function getModel(makeid,yearid) {
  55. var strURL="findCity.php?make="+makeid+"&year="+yearid;
  56. var req = getXMLHTTP();
  57.  
  58. if (req) {
  59.  
  60. req.onreadystatechange = function() {
  61. if (req.readyState == 4) {
  62. // only if "OK"
  63. if (req.status == 200) {
  64. document.getElementById('citydiv').innerHTML=req.responseText;
  65. } else {
  66. alert("There was a problem while using XMLHTTP:\n" + req.statusText);
  67. }
  68. }
  69. }
  70. req.open("GET", strURL, true);
  71. req.send(null);
  72. }
  73.  
  74. }
  75. </script>
  76. </head>
  77. <body>
  78. <form method="post" action="" name="form1">
  79. <table width="60%" border="0" cellspacing="0" cellpadding="0">
  80. <tr>
  81. <td width="150">Make</td>
  82. <td width="150">
  83.  
  84. <?php
  85. $link = mysql_connect('localhost', 'root', '1964vette'); //changet the configuration in required
  86. if (!$link) {
  87. die('Could not connect: ' . mysql_error());
  88. }
  89. mysql_select_db('productionfigures');
  90. $query="SELECT id,makename FROM makes";
  91. $result=mysql_query($query);
  92.  
  93. ?>
  94.  
  95. <select name="makes" onChange="getYear(this.value)">
  96. <option>Select Make</option>
  97. <? while($row=mysql_fetch_array($result)) { ?>
  98. <option value=<?=$row['id']?>><?=$row['makename']?></option>
  99. <? } ?>
  100. </select></td>
  101. </tr>
  102. <tr style="">
  103. <td>Year</td>
  104. <td ><div id="statediv"><select name="years" >
  105. <option>Select Make First</option>
  106. </select></div></td>
  107. </tr>
  108. <tr style="">
  109. <td>Model</td>
  110. <td ><div id="citydiv"><select name="models">
  111. <option>Select Year First</option>
  112. </select></div></td>
  113. </tr>
  114. <tr>
  115. <td>&nbsp;</td>
  116. <td>&nbsp;</td>
  117. </tr>
  118. <tr>
  119. <td>&nbsp;</td>
  120. <td>&nbsp;</td>
  121. </tr>
  122. </table>
  123. </form>
  124. </body>
  125. </html>

Here is findstate.php:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!--//---------------------------------+
  2. // Developed by Roshan Bhattarai |
  3. // http://roshanbh.com.np |
  4. // Contact for custom scripts |
  5. // or implementation help. |
  6. // email-nepaliboy007@yahoo.com |
  7. //---------------------------------+-->
  8. <?
  9. #### Roshan's Ajax dropdown code with php
  10. #### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
  11. #### if you have any problem contact me at http://roshanbh.com.np
  12. #### fell free to visit my blog http://roshanbh.com.np
  13. ?>
  14.  
  15. <? $makeid=intval($_GET['makes']);
  16. $link = mysql_connect('localhost', 'root', '1964vette'); //changet the configuration in required
  17. if (!$link) {
  18. die('Could not connect: ' . mysql_error());
  19. }
  20. mysql_select_db('productionfigures');
  21. $query="SELECT * FROM years WHERE makeid='$makeid'";
  22. $result=mysql_query($query);
  23.  
  24. ?>
  25. <select name="years" onchange="getModel(<?=$make?>,this.value)">
  26. <option>Select Year</option>
  27. <? while($row=mysql_fetch_array($result)) { ?>
  28. <option value=<?=$row['id']?>><?=$row['yearname']?></option>
  29. <? } ?>
  30. </select>
  31.  

And finally here is findcity.php:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!--//---------------------------------+
  2. // Developed by Roshan Bhattarai |
  3. // http://roshanbh.com.np |
  4. // Contact for custom scripts |
  5. // or implementation help. |
  6. // email-nepaliboy007@yahoo.com |
  7. //---------------------------------+-->
  8. <?
  9. #### Roshan's Ajax dropdown code with php
  10. #### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
  11. #### if you have any problem contact me at http://roshanbh.com.np
  12. #### fell free to visit my blog http://php-ajax-guru.blogspot.com
  13. ?>
  14.  
  15. <? $makeid=intval($_GET['makes']);
  16. $yearid=intval($_GET['years']);
  17. $link = mysql_connect('localhost', 'root', '1964vette'); //changet the configuration in required
  18. if (!$link) {
  19. die('Could not connect: ' . mysql_error());
  20. }
  21. mysql_select_db('productionfigures');
  22. $query="SELECT id, modelname FROM models WHERE makeid='$makeid' AND yearid='$yearid'";
  23. $result=mysql_query($query);
  24. ?>
  25.  
  26. <select name="models">
  27. <option>Select Model</option>
  28. <? while($row=mysql_fetch_array($result)) { ?>
  29. <option value><?=$row['modelname']?></option>
  30. <? } ?>
  31. </select>
  32.  

Any help would be appreciated!!

Arthur
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 10
Reputation: SoniaBaby is an unknown quantity at this point 
Solved Threads: 0
SoniaBaby SoniaBaby is offline Offline
Newbie Poster

Re: AJAX & PHP drop down menu problem

 
0
  #2
Oct 24th, 2008
I dont have the time to look at all your code, however I did notice that part of your script uses short PHP tags: <? instead of <?php

Try changing these and test again (this has been a problem encountered several times by myself).


SoniaBaby
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 23
Reputation: forwardlookguy is an unknown quantity at this point 
Solved Threads: 0
forwardlookguy forwardlookguy is offline Offline
Newbie Poster

Re: AJAX & PHP drop down menu problem

 
0
  #3
Oct 26th, 2008
I changed all the short tags and I'm still having the same problem. Any more ideas?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 2
Reputation: upside_down2000 is an unknown quantity at this point 
Solved Threads: 0
upside_down2000 upside_down2000 is offline Offline
Newbie Poster

Re: AJAX & PHP drop down menu problem

 
0
  #4
Feb 27th, 2009
A good solution - uses ASP, but still some groovy AJAX

Create an AJAX Drop Down menu
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC