Hello friends,I have some problem regarding dropdown list.I have 3(Pair of two dropdown list,1st showing categories & 2nd showing subcategories of selected categories)dropdown list having same functionality.When i select a category from 1st dropdownlist,then it showing all the related subcategories of selected category.
the problem is when i am selecting the first dropdown list , by default other two dropdown list value is selected,takes the same value as taken by first dropdown list.
I am using this code
JS script:
<SCRIPT language=JavaScript>
function reload(form)
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='compare.php?cat=' + val ;


PHP Code:
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

if(strlen($cat) > 0 and !is_numeric($cat)){ // to check if $cat is numeric data or not.
echo "Data Error";
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category");

if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory");

echo "<form method=post name=f1 action='compare.php'>";
echo "<select name='cat' onchange=\"reload(this.form)\ onclick\"reload(this.form)\"><option value=''>Select a brand</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
echo "</select>";

echo "<select name='subcat'><option value=''>Select a Model</option>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
echo "<option value=\" onclick=\"reload(this.value)\"></option>";
echo "</select>";
echo "<br/>";
echo "<br/>";

echo "</form>";


Recommended Answers

All 2 Replies

What r u doing with compare.php?
After selecting the first drop down list U have to refresh second drop down list.
Using Ajax to load data to second drop down list .

I think you are making things complicated.
Just use AJAX to load dropdown lists.
Dont forget to put a dummy value as first element and you call ajax in "on cahnge" event.

Just google it you will get many code for ajax.

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.