jbrill Posted July 6, 2007 Share Posted July 6, 2007 I posted a few days ago about this same topic but never got any real answers so im going to post absolutely everything i have in order to get some answers. I am trying to use a "category" drop down (populated from category table in database), once an item is selected, i would like it to populate the sub-category drop down ( populated from subcategory table in database). Im really stuck here and need some help. here is my database table info for the two drop downs. table : category Structure: cat (basic id field) and category ( actual name of the category) table : subcategory Structure: cat ( corresponds with the above "cat" id field) , subc (the main subcategory table id) and subcat ( the real name of the subcategory) any help would be greatly appreciated! i really need to get this done! thanks so much in advance! Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 At the top of the script I presume you're reading the form using $_POST so let's say we're reading the drop-down box called "category" <?php $cat=$_POST['category']; ?> Next, we want to read the categories table and make the drop-down box selections: <?php $htmcats=''; $query=mysql_query("SELECT * FROM `categories` ORDER BY `category` ASC"); while ($fetch=mysql_fetch_assoc($query)) { $htmcats.='<option value="'.$fetch['categoryid'].'"'.($cat==$fetch['categoryid'] ? ' selected="selected"' : '').'>'.$fetch['category'].'</option>'; } ?> That reads through the categories table and builds a string called $htmcats with the options. If the option selected by the user matches one of the categoryid's from the table then it is made selected. Handy if your form validates the data from the user so they don't have to keep selecting the category each time they mess up the form. Next we want to add that list of options onto the code: Category: <select name="category"><?=$htmcats?></select> That's all there is to it. Just adapt the code for the subcategories. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 btw, the above PHP is presuming that your table `categories` has the following fields: categoryid: primary index, integer category: varchar - category name Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 If you want to make it so that the second drop-down box is populated to a separate list of sub categories depending on what the first category was chosen, you'll need Javascript for that. I try and stay clear of Javascript - it bears the mark of the devil Quote Link to comment Share on other sites More sharing options...
jbrill Posted July 6, 2007 Author Share Posted July 6, 2007 They way i want this to work will probably need to include javascript because i dont want anything to appear in the subcategory drop down until somethign is selected from the category drop down. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 Javascript it is then. Funny - I only just PM'd you! Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 You might be better off posting this question in the Javascript forum. Quote Link to comment Share on other sites More sharing options...
jbrill Posted July 6, 2007 Author Share Posted July 6, 2007 somebody here must know javascript! Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 Yeah but its the wrong forum. Its like talking about cars in a birdwatching forum - you wouldn't get many replies! Quote Link to comment 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.