hi friends,

This is my code but it not working properly.pls tell the solution

<?php
mysql_connect("localhost","root","");
mysql_select_db("samples");


?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title></title>

<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value; 
self.location='drop3.php?cat=' + val ;
}
function reload3(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value; 
var val2=form.subcat.options[form.subcat.options.selectedIndex].value; self.location='drop3.php?cat1=' + val + '&cat3=' + val2 ;
}

</script>
</head>

<body>
<?php


// Getting the data from Mysql table for first list box
$query1=mysql_query("SELECT DISTINCT category,cat_id FROM table1 order by category"); 
$cat=$_REQUEST['cat'];
echo $cat;
if(isset($cat) and strlen($cat) > 0)
{
$query2=mysql_query("SELECT DISTINCT subcategory,sub_id FROM table1 where cat_id=$cat order by subcategory"); 
}
else
{
$query2=mysql_query("SELECT DISTINCT subcategory,sub_id FROM table1 order by subcategory");
} 

// end of query for second subcategory drop down list box ///////////////////////////



$cat3=$_REQUEST['cat1'];
echo $cat3;
if(isset($cat3) and strlen($cat3) > 0)


{
$query3=mysql_query("SELECT DISTINCT `titles` FROM `table1` WHERE `sub_id`= $cat3 order by titles"); 
}
else
{
$query3=mysql_query("SELECT DISTINCT titles FROM table1 order by titles"); 
}

// end of query for third subcategory drop down list box 


echo "<form method=post name=f1 action='drop33.php'>";
//    Starting of first drop downlist 
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($fetch1 = mysql_fetch_array($query1)) 
{ 
if($fetch1['cat_id']==@$cat)
{
echo "<option selected value='$fetch1[cat_id]'>$fetch1[category]</option>"."<BR>";
}
else
{
echo  "<option value='$fetch1[cat_id]'>$fetch1[category]</option>";
}
}
echo "</select>";


//  Starting of second drop downlist 
echo "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";
while($fetch2 = mysql_fetch_array($query2)) 
{ 
if($fetch2['cat_id']==@$cat3)
{
echo "<option selected value='$fetch2[sub_id]'>$fetch2[subcategory]</option>"."<BR>";
}
else
{
echo  "<option value='$fetch2[sub_id]'>$fetch2[subcategory]</option>";
}
}
echo "</select>";



//Starting of third drop downlist 
echo "<select name='subcat3' ><option value=''>Select one</option>";
while($fetch3 = mysql_fetch_array($query3))
 { 
echo  "<option value='$fetch3[titles]'>$fetch3[titles]</option>";
}
echo "</select>";



echo "<input type=submit value='Submit the form data'></form>";
?>

</body>

</html>

database table:

id cat_id category sub_id
1 1 Websites/IT and Softwares acc1
2 1 Websites/IT and Softwares black
3 1 Websites/IT and Softwares black
4 2 Writing and Content act

subcategory title

accessdb linkdatabase
BlackBerry Advertising
BlackBerry googlecalender
accademic good english writer
writing
AccademicWriting
good english writer

Recommended Answers

All 4 Replies

You should not use the retrieved data in double quotes, that is wrong method of implementation, try like this..

if($fetch1['cat_id']==@$cat)
{
echo "<option selected value='".$fetch1['cat_id']."'>".$fetch1['category']."</option>"."<BR>";
}
else
{
echo  "<option value='".$fetch1['cat_id']."'>".$fetch1['category']."</option>";
}


if($fetch2['cat_id']==@$cat3)
{
echo "<option selected value='".$fetch2['sub_id']."'>".$fetch2['subcategory']."</option>"."<BR>";
}
else
{
echo  "<option value='".$fetch2['sub_id']."'>".$fetch2['subcategory']."</option>";
}

Also can you tell me what you are trying to do? because I didn't get Why do you need that many loops and if conditions.

Member Avatar for diafol

You should not use the retrieved data in double quotes, that is wrong method of implementation, try like this..

... echo "<option selected value='".$fetch1['cat_id']."'>".$fetch1['category']."</option>"."<BR>";

Yes you can do it, as long as you use braces {}:

... echo "<option selected value=\"{$fetch1['cat_id']}\">{$fetch1['category']}</option><br />";

Yes you can do it, as long as you use braces {}:

... echo "<option selected value=\"{$fetch1['cat_id']}\">{$fetch1['category']}</option><br />";

you I agree with you, you are right, but srya hasn't used brace...:icon_smile:

sir,
can you tell me the exact code in php of two drop down list bind together with mysql and search according to that from database.
2nd ddl value changed according to 1st one and then search ???

Manish Jha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.