Jump to content

can someone PLEASE help me with drops downs, auto-populating from database


jbrill

Recommended Posts

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!  :)

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.