| | |
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
![]() |
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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:
Here is findstate.php:
And finally here is findcity.php:
Any help would be appreciated!!
Arthur
Here's the main page:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <title>Roshan's Triple Ajax dropdown code</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="javascript" type="text/javascript"> // Roshan's Ajax dropdown code with php // This notice must stay intact for legal use // Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com // If you have any problem contact me at http://roshanbh.com.np function getXMLHTTP() { //fuction to return the xml http object var xmlhttp=false; try{ xmlhttp=new XMLHttpRequest(); } catch(e) { try{ xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e1){ xmlhttp=false; } } } return xmlhttp; } function getYear(makeid) { var strURL="findState.php?make="+makeid; var req = getXMLHTTP(); if (req) { req.onreadystatechange = function() { if (req.readyState == 4) { // only if "OK" if (req.status == 200) { document.getElementById('statediv').innerHTML=req.responseText; } else { alert("There was a problem while using XMLHTTP:\n" + req.statusText); } } } req.open("GET", strURL, true); req.send(null); } } function getModel(makeid,yearid) { var strURL="findCity.php?make="+makeid+"&year="+yearid; var req = getXMLHTTP(); if (req) { req.onreadystatechange = function() { if (req.readyState == 4) { // only if "OK" if (req.status == 200) { document.getElementById('citydiv').innerHTML=req.responseText; } else { alert("There was a problem while using XMLHTTP:\n" + req.statusText); } } } req.open("GET", strURL, true); req.send(null); } } </script> </head> <body> <form method="post" action="" name="form1"> <table width="60%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="150">Make</td> <td width="150"> <?php $link = mysql_connect('localhost', 'root', '1964vette'); //changet the configuration in required if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('productionfigures'); $query="SELECT id,makename FROM makes"; $result=mysql_query($query); ?> <select name="makes" onChange="getYear(this.value)"> <option>Select Make</option> <? while($row=mysql_fetch_array($result)) { ?> <option value=<?=$row['id']?>><?=$row['makename']?></option> <? } ?> </select></td> </tr> <tr style=""> <td>Year</td> <td ><div id="statediv"><select name="years" > <option>Select Make First</option> </select></div></td> </tr> <tr style=""> <td>Model</td> <td ><div id="citydiv"><select name="models"> <option>Select Year First</option> </select></div></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> </tr> </table> </form> </body> </html>
Here is findstate.php:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!--//---------------------------------+ // Developed by Roshan Bhattarai | // http://roshanbh.com.np | // Contact for custom scripts | // or implementation help. | // email-nepaliboy007@yahoo.com | //---------------------------------+--> <? #### Roshan's Ajax dropdown code with php #### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com #### if you have any problem contact me at http://roshanbh.com.np #### fell free to visit my blog http://roshanbh.com.np ?> <? $makeid=intval($_GET['makes']); $link = mysql_connect('localhost', 'root', '1964vette'); //changet the configuration in required if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('productionfigures'); $query="SELECT * FROM years WHERE makeid='$makeid'"; $result=mysql_query($query); ?> <select name="years" onchange="getModel(<?=$make?>,this.value)"> <option>Select Year</option> <? while($row=mysql_fetch_array($result)) { ?> <option value=<?=$row['id']?>><?=$row['yearname']?></option> <? } ?> </select>
And finally here is findcity.php:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!--//---------------------------------+ // Developed by Roshan Bhattarai | // http://roshanbh.com.np | // Contact for custom scripts | // or implementation help. | // email-nepaliboy007@yahoo.com | //---------------------------------+--> <? #### Roshan's Ajax dropdown code with php #### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com #### if you have any problem contact me at http://roshanbh.com.np #### fell free to visit my blog http://php-ajax-guru.blogspot.com ?> <? $makeid=intval($_GET['makes']); $yearid=intval($_GET['years']); $link = mysql_connect('localhost', 'root', '1964vette'); //changet the configuration in required if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('productionfigures'); $query="SELECT id, modelname FROM models WHERE makeid='$makeid' AND yearid='$yearid'"; $result=mysql_query($query); ?> <select name="models"> <option>Select Model</option> <? while($row=mysql_fetch_array($result)) { ?> <option value><?=$row['modelname']?></option> <? } ?> </select>
Any help would be appreciated!!
Arthur
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: clear-remove all child nodes in javascript
- Next Thread: IE auto upper case?
Views: 2804 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxexample ajaxjspservlets api blackjack browser bug captchaformproblem checkbox child class close cookies createrange() cursor date debugger dependent developer disablefirebug dom dropdown editor element embed engine events explorer ext file flash form forms game gears getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe images internet java javascript javascripthelp2020 jquery jsf jsfile jump libcurl maps margin math matrixcaptcha media mp3 mysql object onerror onmouseoutdivproblem onreadystatechange parent passing paypal pdf php player position post programming progressbar rated runtime safari scriptlets scroll search security session shopping size software solutions star stars stretch synchronous tweet unicode web webkit webservice window wysiwyg \n





