Jump to content

Dynamic Dropdown Menu


adilamarwat
 Share

Recommended Posts

I have database in mysql name "prj"contains 1 table name "inf".which have 2 fields named "student" & "teacher".I want to create two drop down menu. 1 for : Select Category [student] or [Teacher]. when i select student it automatically selects the field of student from database and display in the 2nd dropdown menu. How can i do this , please help me in this regard.

Link to comment
Share on other sites

Ok. I am writing down the codes but it's a sample. Change the required configuration in your database table.1. For index.php page:

<?php$dbservertype='mysql';$servername='localhost';// username and password to log onto db server$dbusername='root';$dbpassword='password';// name of database$dbname='drop_down';////////////////////////////////////////////// DONOT EDIT BELOW ////////////////////////////////////////////////connecttodb($servername,$dbname,$dbusername,$dbpassword);function connecttodb($servername,$dbname,$dbuser,$dbpassword){global $link;$link=mysql_connect ("$servername","$dbuser","$dbpassword");if(!$link){die("Could not connect to MySQL");}mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());}//////// End of connecting to database ////////?><!doctype html public "-//w3c//dtd html 3.2//en"><html><head><title>Multiple drop down list box from plus2net</title><script language=JavaScript>function reload(form){var val=form.cat.options[form.cat.options.selectedIndex].value;self.location='index.php?cat=' + val ;}</script></head><body><?@$cat=$_GET['cat']; // Use this line or below line if register_global is offif(strlen($cat) > 0 and !is_numeric($cat)){ // to check if $cat is numeric data or not. echo "Data Error";exit;}///////// Getting the data from Mysql table for first list box//////////$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); ///////////// End of query for first list box/////////////////// for second drop down list we will check if category is selected else we will display all the subcategory///// 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"); } ////////// end of query for second subcategory drop down list box ///////////////////////////echo "<form method=post name=f1 action='index.php'>";/// Add your form processing page address to action in above line. Example action=dd-check.php////////////// Starting of first drop downlist /////////echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</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>";////////////////// This will end the first drop down list ///////////////////// Starting of second drop downlist /////////echo "<select name='subcat'><option value=''>Select one</option>";while($noticia = mysql_fetch_array($quer)) { echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";}echo "</select>";////////////////// This will end the second drop down list /////////////// Add your other form fields as needed here/////echo "<input type=submit value=Submit>";echo "</form>";?></body></html>
2. Database sql file:
CREATE TABLE category ( cat_id int(2) NOT NULL auto_increment, category varchar(25) NOT NULL default '', PRIMARY KEY (cat_id) ) TYPE=MyISAM; # # Dumping data for table `category` # INSERT INTO category VALUES (1, 'Fruits'); INSERT INTO category VALUES (2, 'Colors'); INSERT INTO category VALUES (3, 'Games'); INSERT INTO category VALUES (4, 'Vehicles'); # -------------------------------------------------------- # # Table structure for table `subcategory` # CREATE TABLE subcategory ( cat_id int(2) NOT NULL default '0', subcategory varchar(25) NOT NULL default '' ) TYPE=MyISAM; # # Dumping data for table `subcategory` # INSERT INTO subcategory VALUES (1, 'Mango'); INSERT INTO subcategory VALUES (1, 'Banana'); INSERT INTO subcategory VALUES (1, 'Orange'); INSERT INTO subcategory VALUES (1, 'Apple'); INSERT INTO subcategory VALUES (2, 'Red'); INSERT INTO subcategory VALUES (2, 'Blue'); INSERT INTO subcategory VALUES (2, 'Green'); INSERT INTO subcategory VALUES (2, 'Yellow'); INSERT INTO subcategory VALUES (3, 'Cricket'); INSERT INTO subcategory VALUES (3, 'Football'); INSERT INTO subcategory VALUES (3, 'Baseball'); INSERT INTO subcategory VALUES (3, 'Tennis'); INSERT INTO subcategory VALUES (4, 'Cars'); INSERT INTO subcategory VALUES (4, 'Trucks'); INSERT INTO subcategory VALUES (4, 'Blkes'); INSERT INTO subcategory VALUES (4, 'Train');
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...