| | |
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: 2812 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxcode ajaxhelp animate array automatically autoplay beta boarder box bug button calendar captcha card cart codes column cookies createrange() css cursor date debugger decimal design developer dom download dropdown element enter error events firefox firehose flash focus form frameworks getselection google gwt html htmlform iframe image() index java javascript javascripts jawascriptruntimeerror jquery jsp listbox maps marquee masterpage menu microsoft mimic mp3 mp4 offline onmouseover parameters php player post problem programming progressbar prototype rating redirect regex safari scale scriptlets search select size sources sql starrating text textarea toggle twitter validation variables w3c web website window windowofwords windowsxp xml xspf





