dflow Posted November 5, 2009 Share Posted November 5, 2009 i managed to get this script working i have a Category tbl in the first dropdown and the second dropdown needs to be selected according to CategoryID the result i get is if a categoryID=2 is selected i get: the second dropdown is populated correctly with the city_list but the first selected dropdown is then reset with no value/label showing the result url parameter dd3.php?cat=2 then if i select the second drop down with CityID=1 it is reset with the url parameter showing dd3.php?cat=&cat3=1(the selected CityID) the idea is to continue to a third dropdown list here is the code: <html> <head> <SCRIPT language=JavaScript> function reload(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; self.location='dd3.php?cat=' + val ; } function reload3(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; var val2=form.subcat.options[form.subcat.options.selectedIndex].value; self.location='dd3.php?cat=' + val + '&cat3=' + val2 ; } </script> </head> <body> <?php echo "<form method=post name=f1 action='dd3ck.php'>"; $quer2=mysql_query("SELECT CategoryName,CategoryID FROM category_list order by CategoryName"); echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>"; while($noticia2 = mysql_fetch_array($quer2)) { if($noticia2['CategoryID']==@$cat){echo "<option selected value='$noticia2[CategoryID]'>$noticia2[CategoryName]</option>"."<BR>";} else{echo "<option value='$noticia2[CategoryID]'>$noticia2[CategoryName]</option>";} } echo "</select>"; // second drop down list $cat=$HTTP_GET_VARS['cat']; echo "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>Select one</option>"; switch ($cat) { case 1: $quer=mysql_query("SELECT * FROM country_list order by CountryName"); while($noticia = mysql_fetch_array($quer)) { echo "<option value='$noticia[CountryID]'>$noticia[CountryName]</option>"; } case 2: $quer=mysql_query("SELECT * FROM city_list order by CityName"); while($noticia = mysql_fetch_array($quer)) { echo "<option value='$noticia[CityID]'>$noticia[CityName]</option>"; } case 3: $quer=mysql_query("SELECT * FROM country_list order by CountryName"); while($noticia = mysql_fetch_array($quer)) { echo "<option value='$noticia[CountryID]'>$noticia[CountryName]</option>"; } } echo "</select>"; ?> <?php echo "<input type=submit value='Submit the form data'></form>"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/180455-populating-dropdown-help-with-script/ Share on other sites More sharing options...
Psycho Posted November 5, 2009 Share Posted November 5, 2009 You don't define $cat (which you use to set the selected category option) until after you have created the category list! I went ahead and cleaned up the code <?php //Get selected category $cat = (isset($_GET['cat']) ? $_GET['cat'] : false; //Generate category options $catOptions = ''; $query = "SELECT CategoryName,CategoryID FROM category_list order by CategoryName"; $result = mysql_query($query); while($option = mysql_fetch_array($result)) { $selected = ($option['CategoryID']==$cat) ? ' selected="selected"': ''; $catOptions .= "<option value=\"{$option['CategoryID']}\"{$selected}>{$option['CategoryName']}</option>\n"; } //Generate subcategory options $subcatOptions = ''; $query = false; switch ($cat) { case 1: case 3: $query = "SELECT CountryID as value, CountryName as text FROM country_list ORDER BY CountryName"; break; case 2: $query = "SELECT CityID as value, CityName as text FROM city_list ORDER BY CityName"; break; } if ($query!==false) { $result = mysql_query($query); while($option = mysql_fetch_array($result)) { $subcatOptions .= "<option value=\"{$option['value']}\">{$option['text']}</option>\n"; } } ?> <html> <head> <SCRIPT language=JavaScript> function reload(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; self.location='dd3.php?cat=' + val ; } function reload3(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; var val2=form.subcat.options[form.subcat.options.selectedIndex].value; self.location='dd3.php?cat=' + val + '&cat3=' + val2 ; } </script> </head> <body> <form method="post" name="f1" action="dd3ck.php"> <select name="cat" onchange="reload(this.form);"> <option value="">Select one</option> <?php echo $catOptions; ?> </select> <select name="subcat" onchange="reload3(this.form);"> <option value=''>Select one</option> <?php echo $subcatOptions; ?> </select> <input type="submit" value="Submit the form data"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/180455-populating-dropdown-help-with-script/#findComment-951984 Share on other sites More sharing options...
dflow Posted November 5, 2009 Author Share Posted November 5, 2009 i get unexpected ';' on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/180455-populating-dropdown-help-with-script/#findComment-952016 Share on other sites More sharing options...
dflow Posted November 6, 2009 Author Share Posted November 6, 2009 appreciate the help Quote Link to comment https://forums.phpfreaks.com/topic/180455-populating-dropdown-help-with-script/#findComment-952205 Share on other sites More sharing options...
Psycho Posted November 6, 2009 Share Posted November 6, 2009 As my sig states there will likely be syntax errors. I write the code with the logic in mind. Since I don't hae your database - and I'm too lazy to create one - I did not test the code. Try changing this $cat = (isset($_GET['cat']) ? $_GET['cat'] : false; To this $cat = (isset($_GET['cat'])) ? $_GET['cat'] : false; There's a good chance there are other syntax errors as well. Quote Link to comment https://forums.phpfreaks.com/topic/180455-populating-dropdown-help-with-script/#findComment-952287 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.