Jump to content

Dependent Drop Down


tmattevc

Recommended Posts

So I have 2 seemingly simple questions. I am working on a dynamic drop down menu where there are 2 drop downs the second one being dependent on the first one.

 

I am using PHP / MySQL

 

QUESTIONS:

1. I want to make it required that user has selected something from BOTH drop downs before the results are displayed.

 

2. This HAS to be really easy but I can't figure it out, not sure if this is a php fix or a database fix:

Once my results are displayed, I am printing the name of selected category (first drop down) and subcategory (2nddrop down). The sub category ($subcat) is printing correctly as the value I have entered in the database for subcategory but the main category ($cat) is printing the cat_id value.

 

For example I am getting: 1 - Coatings where 1 is the main category cat_id and Coatings is the subcategory value. What i need is for it to print something like: United Kingdom - Coatings

 

CODE:

 

1. This is in the page on my site (PHP and JAVA):

 

<SCRIPT language=JavaScript>

function reload(form)

{

var val=form.cat.options[form.cat.options.selectedIndex].value;

self.location='contact-test2.php?cat=' + val ;

}

 

</script>

 

<?

@$cat=$_GET['cat'];

$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");

}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); }

 

echo "<form method=post name=f1 action='find-your-direct-contact.php'>";

 

echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select Country</option>";

while($noticia2 = mysql_fetch_array($quer2)) {

if($noticia2['cat_id']==@$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 Product</option>";

while($noticia = mysql_fetch_array($quer)) {

echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";

}

echo "</select>";

 

 

echo "<input type=submit value=Submit>";

echo "</form>";

?>

 

2. This is in the page that prints results:

 

<?

// This prints the Country and Product Selected //

 

$cat=$_POST['cat'];

$subcat=$_POST['subcat'];

echo "$cat $subcat ";

?>

 

(along with some if / else statements)

Link to comment
https://forums.phpfreaks.com/topic/151306-dependent-drop-down/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.