digitalgod Posted July 25, 2006 Share Posted July 25, 2006 hey guys,I've been pulling my hair out trying to figure this out...I have 2 drop downs, first 1 is populated from a db, second one displays the sub categories of what has been selected in the first drop downhere's what I got so far[code]<?php$sql_clubs='SELECT * FROM clubs ORDER BY name';$req_clubs=mysql_query($sql_clubs) or die(query_error());$numRows = mysql_num_rows($req_clubs); $clubList = array();?><script type="text/javascript">function fillclub(){ // this function is used to fill the club list on load<?phpwhile($data = mysql_fetch_assoc($req)) { array_push($clubList,$data['name']); echo 'addOption(document.form.club, "'.$data['name'].'", "'.$data['name'].'", "");';}?>}function Selectnight(){// ON selection of club this function will workremoveAllOptions(document.form.night);addOption(document.form.night, "", "Choose one", "");<?phpfor ($i=0;$i<=$numRows;$i++) { $sql_nights='SELECT * FROM clubnights WHERE club = '.$clubList[$i].'ORDER BY night'; $req_nights=mysql_query($sql_nights) or die(query_error()); $night_data = mysql_fetch_array($req_nights); echo 'if(document.form.club.value == '.$clubList[$i].'){ addOption(document.form.night,"'.$night_data['night'].'", "'.$night_data['night'].'"); }';}?>function removeAllOptions(selectbox){ var i; for(i=selectbox.options.length-1;i>=0;i--) { //selectbox.options.remove(i); selectbox.remove(i); }}function addOption(selectbox, value, text ){ var optn = document.createElement("OPTION"); optn.text = text; optn.value = value; selectbox.options.add(optn);}[/code]but it's not working... any ideas what's wrong? Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/ Share on other sites More sharing options...
thepip3r Posted July 25, 2006 Share Posted July 25, 2006 this would be more of a javascript question than a PHP one; but, considering you don't have any <html> or <head> tags anywhere (where your javascript functions MAY need to go) that would be the first place i'd look.edit:AND this is not correct:[code]or die(query_error());[/code]it should be:[code]or die(mysql_error());[/code]edit 2:unless you have $req called somewhere else in the page that you didn't copy and paste, it looks like your loop should be mysql_fetch_assoc'ing ($req_clubs) instead of just $req...[code]<?php$sql_clubs='SELECT * FROM clubs ORDER BY name';$req_clubs=mysql_query($sql_clubs) or die(query_error());$numRows = mysql_num_rows($req_clubs); $clubList = array();?><script type="text/javascript">function fillclub(){ // this function is used to fill the club list on load<?phpwhile($data = mysql_fetch_assoc($req)) { array_push($clubList,$data['name']); echo 'addOption(document.form.club, "'.$data['name'].'", "'.$data['name'].'", "");';}?>[/code]should look like:[code]<?php$sql_clubs='SELECT * FROM clubs ORDER BY name';$req_clubs=mysql_query($sql_clubs) or die(query_error());$numRows = mysql_num_rows($req_clubs); $clubList = array();?><script type="text/javascript">function fillclub(){ // this function is used to fill the club list on load<?phpwhile($data = mysql_fetch_assoc($req_clubs)) { array_push($clubList,$data['name']); echo 'addOption(document.form.club, "'.$data['name'].'", "'.$data['name'].'", "");';}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63662 Share on other sites More sharing options...
digitalgod Posted July 25, 2006 Author Share Posted July 25, 2006 well I only placed the part that I know has errors in, the form itself is just 2 drop downs and a submit button... first drop down has a onChange="fillnight();"I think it's php related because I'm positive I'm doing something wrong in php, it works fine if I use predefined values and only javascript Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63664 Share on other sites More sharing options...
thepip3r Posted July 25, 2006 Share Posted July 25, 2006 FYI... you can also abbreviate your arraypush() to:[code]$clubList[] = $data['name'];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63667 Share on other sites More sharing options...
digitalgod Posted July 25, 2006 Author Share Posted July 25, 2006 thanks thepip3r but it's still not working...*edit*I know it's this part that doesn't work because if I take it off the page is displayed...[code]<?phpfor ($i=0;$i<=$numRows;$i++) { $sql_nights='SELECT * FROM clubnights WHERE club = '.$clubList[$i].'ORDER BY night'; $req_nights=mysql_query($sql_nights) or die(query_error()); $night_data = mysql_fetch_assoc($req_nights); echo 'if(document.form.club.value == '.$clubList[$i].'){ addOption(document.form.night,"'.$night_data['night'].'", "'.$night_data['night'].'"); }';}?>[/code]still can't find why though Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63672 Share on other sites More sharing options...
ryanlwh Posted July 25, 2006 Share Posted July 25, 2006 you have to check the selectedIndex for drop down lists Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63686 Share on other sites More sharing options...
digitalgod Posted July 25, 2006 Author Share Posted July 25, 2006 [quote author=ryanlwh link=topic=101844.msg403483#msg403483 date=1153866452]you have to check the selectedIndex for drop down lists[/quote]how do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63690 Share on other sites More sharing options...
ryanlwh Posted July 25, 2006 Share Posted July 25, 2006 try[code]echo 'if (document.form.club.options[document.form.club.selectedIndex].value == '.$clubList[$].'){ [/code]adopted from http://www.pageresource.com/jscript/jdropbox.htm Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63692 Share on other sites More sharing options...
digitalgod Posted July 25, 2006 Author Share Posted July 25, 2006 still not working... Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63695 Share on other sites More sharing options...
ryanlwh Posted July 25, 2006 Share Posted July 25, 2006 need a space before ORDER, and add quotes too:[code]$sql_nights='SELECT * FROM clubnights WHERE club = "'.$clubList[$i].'" ORDER BY night';[/code]Now if $clubList contains strings:[code]echo 'if (document.form.club.options[document.form.club.selectedIndex].value == "'.$clubList[$].'"){ [/code] Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63701 Share on other sites More sharing options...
digitalgod Posted July 25, 2006 Author Share Posted July 25, 2006 I think we're getting somewhere, the page is displayed but there's nothing in the drop downs, it's giving me a javascript error saying that there's something wrong hereselectbox.options.add(optn);what's wrong now? :(by the way is that part any good[code]<?phpfor ($i=0;$i<=$numRows;$i++) { $sql_nights='SELECT * FROM mtlguest_clubnights WHERE club = "'.$clubList[$i].'" ORDER BY night'; $req_nights=mysql_query($sql_nights) or die(query_error()); $night_data = mysql_fetch_assoc($req_nights); echo 'if (document.form.club.options[document.form.club.selectedIndex].value == "'.$clubList[$i].'"){ addOption(document.form.night,"'.$night_data['night'].'", "'.$night_data['night'].'"); }';}?>[/code]I have a feeling that it will only display 1 sub category... Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63711 Share on other sites More sharing options...
ryanlwh Posted July 25, 2006 Share Posted July 25, 2006 do you have document.form.night in your document? For the subcat thing... maybe you want to loop through the records?? Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63716 Share on other sites More sharing options...
digitalgod Posted July 25, 2006 Author Share Posted July 25, 2006 yes I do have document.form.night[code]<div id="article"><form name="form" id="form" action="admin.php?a=delclubnight" method="post"><input type="hidden" name="process" value="yes" /> <table border="0"> <tr> <td>Club Name:</td> <td><select name="club" id="club" onChange="Selectnight();"> <option>Choose one</option> </select></td> </tr> <tr> <td>Night::</td> <td><select name="night" id="night"> <option>Choose one</option> </select></td> </tr> </table><br /><input type="submit" value="Delete Night" /></form></div>[/code]but it's the first drop down that isn't displaying anything anymore, If I remove the part of code that populates the subcat the first drop down shows me the list of clubs available in the database Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-63719 Share on other sites More sharing options...
digitalgod Posted July 26, 2006 Author Share Posted July 26, 2006 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/15626-drop-down-with-db/#findComment-64184 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.