Forum Moderators: coopster

Message Too Old, No Replies

Limit choice in 2nd drop down list.

         

egibberate

9:11 pm on Dec 9, 2008 (gmt 0)

10+ Year Member



Hi,
I have two drop down lists in my form which are automatically populated with data from two mySQL tables. The user makes selections and the records are added to the database.
The lists are for 'category' & sub_category'
I would like to restrict the choice in the 'sub_category' list based upon what was selected from the 'category' list.
Is it possible to do this when I have dynamically populated drop down lists?
I am a beginner & would appreciate it very much if anyone knows of some sample code for this that I could learn from.
Thank you for your help and expertise.

lokeshshettyk

3:53 pm on Dec 10, 2008 (gmt 0)

10+ Year Member



On change of the first select box you may want to reload the form and update the second select box based on the selection from the 1st select box. I would prefer not to load the second select box while the page is loaded for the 1st time.......

Pico_Train

4:42 pm on Dec 10, 2008 (gmt 0)

10+ Year Member



onChange implies you will probably need to do some AJAX. Look it up you will need to have a few javascript files to get you going with this but the below should help too. If you need these files, send me a personal message and I will point you to my page you can steal these from. Steal meaning find inspiration.

category db -> category_id, category
sub_category db -> sub_id, sub_category, category_id_fk

<select name="category" id="category" onchange="getSubCat()">
<option value="category_id">Category</option>
</select>

Querying with MySQL to get sub-categories of category chosen.

function getSubCat()
{
subcat = array();
$sql = 'SELECT sc.sub_id, sc.sub_category, sc.category_id_fk , c.category from category as c, sub_category as sc WHERE c.category_id = sc.category_id_fk ORDER BY sc.sub_category ASC

$result = mysql_query() ; blah blah blah whichever way you do it

return $result;
}