Jump to content

subcategory dropdown based on main category


Recommended Posts

thanks for your previous replies,

now am designing a page called add-product as a part of the project 'product management store'

my problem is how to design dropdown based on the previour dropdown selection (using php and mysql)


(i.e according to my database and table data

if i select electronic goods from category drop down i should be able to see only items pertained to electronic items in the second drop down menu


category - electonic goods


subcategory - laptops




should be there...but in my design its showing all subcategories...i have attached my database file and add-product.php file for your review...plz reply soon.......................


AND also suggest me how to upload image and handling image



Link to comment
Share on other sites

You don't have to use PHP. In fact, it may be better to use client-side processing as it's only applicable to the viewer who's looking at that page at the time.


This javascript chained select changes the options in the second box depending on what you selected in the first box, and similarly for the third.


Link to comment
Share on other sites

i agree to you,please have a look at my code, i have to retrive data from mysql table to the form ,in my code i have successfully retrieved data from category table,am failing to retrieve data from subcategory table which should be based on the category selected item.....please help me

Link to comment
Share on other sites

i have uploaded add-product.php in my previous post and made some modificatiosns,also pasted part of the code with some modifications...for quick review


///category code part in add-product.php////

<!--product category-->


<td style="font-family:verdana;color:gray;font-size:.8pc;font-weight:bold;"width = 50%>Product Category:</td>

<td width = 50%>


$ddone = @mysql_query("SELECT cat_id,cat_name FROM category");

echo "<select name = 'maincat'>";

while($row = mysql_fetch_assoc($ddone)){

$catid = $row['cat_id'];

$catname = $row['cat_name'];

echo "<option value = \"$catid\"> $catname </option>";


echo "</select>";




///ends here




///subcategory followed by above in php file

<!--product sub-category-->


<td style="font-family:verdana;color:gray;font-size:.8pc;font-weight:bold;"width = 50%>Product Sub-category:</td>



$ddtwo = @mysql_query("SELECT cat_id,subcat_name FROM subcategory WHERE cat_id = $catid");

echo "<select name = 'subcat'>";

while($now = mysql_fetch_assoc($ddtwo)){

$subcatid = $now['subcat_id'];

$subcatname = $now['subcat_name'];

echo "<option value = \"$subcatid\">$subcatname</option>";


echo "</select>";




///subcategory ends here




now my problem is irrespective of main category selection it showing subcategory as tables and chairs but it shd change according to main category selection,plz have a look at my code and give me solution

(i have uploaded my database i.e products.sql in my previous post)

i think am failing in catching the first dropdown menu value or displaying in second dropdown ....

for my above result it must have always selecting cat_id as 4 it seems....


sir.............plz plz help me

Link to comment
Share on other sites

  • 1 year later...

How we use the sqlquery please tell me there was no error in this code but it does not fetch the data from database why ????


$myconnect=mysql_connect("localhost","root","")or die("does not connect to mysql");

$db=mysql_select_db('billsystem') or die('does not connect to database');

$result="select clientname from addclient";



<form action="searchbyclientbillform11.php" method="post" style="margin-left:20px">


<select name="clientname">




foreach($res as $field)


//echo $field;




<option value="<?php echo $field; ?>"><?php echo $field; ?> </option>





//echo $res




<input type="submit" name="sub" width="30%" value="show" />




$myconnect=mysql_connect("localhost","root","")or die("does not connect to mysql");

$db=mysql_select_db('billsystem')or die("does not connect to database");

$result="select addclient.userid,addclient.sno,addnewbill.userid,addnewbill.invoiceno,addnewbill.amount,



,monthlystatement.grandtotal from addclient,addnewbill,

monthlystatement WHERE addclient.clientname='clientname'";


echo "<table border='1'>";

echo"<tr><th>userid</th><th>Client Name</th><th>SNO</th><th>invoiceno</th><th>amount</th><th>Date</th><th>Orderno</th><th>Descripition</th><th>Rate</th><th>totalamount</th><th>servicetax</th><th>grandtotal </th></tr>";



// Print out the contents of each row into a table

echo "<tr>";

echo "<td>" . $row->userid . "</td>";

echo "<td>" . $row->clientname . "</td>";

echo "<td>" . $row->sno . "</td>";

echo "<td>" . $row->invoiceno . "</td>";

echo "<td>" . $row->amount . "</td>";

echo "<td>" . $row->date . "</td>";

echo "<td>" . $row->orderno . "</td>";

echo "<td>" . $row->descripition. "</td>";

echo "<td>" . $row->rate . "</td>";

echo "<td>" . $row->totalamount . "</td>";

echo "<td>" . $row->servicetax . "</td>";

echo "<td>" . $row->grandtotal . "</td>";

echo "<td><a href='recordshiform.php?userid=" . $row->userid . "'>GO TO FORM</a></td>";





echo "</table>";



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...