View Full Version : how do i get subcategories by selecting categories in select option by javascript

Jun 9th, 2008, 08:10 AM
i have two select boxes..
one for main category and another for the subcategory.
when i select any category from main category select box , it calls the javascript function and brings the subcategories of the selected main categroy.
i am having problem in generating subcategories by javascript.
i can generate it by php code but my requirement is to generate it by javascript. any solution guys..
here is the example what am i asking

<script language="javascript">
function updatecategory(selectObj){
var idx = selectObj.selectedIndex;
var which = selectObj.options[idx].value;
var cLists=categoryLists(which);

function categoryLists(catid) {
alert(dont know what to do here in this function);
return catid ;

<form name="RTEDemo" id="RTEDemo" method="post" >
<select name="maincategory" id="maincategory" onChange="updatecategory(this);">
<option value=" ">-------</option>
<? $tq="SELECT * FROM `categories` ORDER BY `name` ASC ";
$rtq=mysql_query($tq) or die("Erorr in $tq".mysql_error($rtq));

while ($op=mysql_fetch_array($rtq))
echo "<option value='$op[0]'>$op[1]</option>";

<select name="subcategory" id="subcategory" ><option value="0">-------</option>
$qo="select * from `subcategory` where `categoryname`='$nam[1]'";
$qoe=mysql_query($qo) or die("error$qo".mysql_error());
$qo="select * from `subcategory` where `categoryid`='$scat'";
$qoe=mysql_query($qo) or die("error$qo".mysql_error());
while($subcat=mysql_fetch_array($qoe)) {
echo "<option value=\"$subcat[0]\">$subcat[3]</option>";
}//end while ?>

please help me. im new in javascripting.