Hi, This is a code of Dynamic Drop Down Option.
If I select Item Code It Shows Item Name and vice versa.
I need to fill option from MySQL Database. Can anybody help me out. please .

<html>
<head>
<title>Gender  Drop Down Test</title>
<script type="text/javascript">
function selectItem(value, id){document.getElementById(id).value=value;}
</script>
</head>
<body>
I'm a 
<select name='item' id='ItemCode' onChange="selectItem(this.value, 'ItemName');">
  <option value='0'></option>

</select>.<br />
<p>
<select name='item' id='ItemName' onChange="selectItem(this.value, 'ItemCode')">
  <option value='0'></option>
  
</select>
</p>
</body>
</html>

Thanks
Ayesha

Recommended Answers

All 11 Replies

Hey.
Just guessing here since im not really sure what you want....
So just fill in the blanks from ur DB table.

SELECT {table_column_here} FROM {your_table_name} WHERE {what_you_want}

If you can supply the table details i can help you complete this

Are you using any server side scripting...
Please give some more info

<? 
$cres=mysql_query("SELECT * FROM table  ORDER BY id ");
$ccunt=mysql_num_rows($cres);
	echo "<script language=javascript>\n";
	echo "function getsubcats() {\n";
		echo "var d=document.frmaddpro;\n";
		echo "if(d.selcategory.value==0) {\n";
			echo "d.selsubcat.options.length = 0;\n";
			echo "d.selsubcat.options[0]=new Option(\"Select Subcategory\",\"\")\n";
		echo "}\n";
	while($row_cat=mysql_fetch_array($cres,MYSQL_BOTH)){
		echo "if(d.selcategory.value==".$row_cat['id'].") {\n";
				echo "d.selsubcat.length=0;\n";
				$sub_res=mysql_query("SELECT * FROM table WHERE codeid='".$row_cat['id']."'");  
				$i=1;
				echo "d.selsubcat.options[0]=new Option(\"Select Subcategory \",\"\")\n";
				while($sub_row=mysql_fetch_array($sub_res,MYSQL_BOTH)){
					echo "d.selsubcat .options[".$i."]=new Option('".$sub_row['name']."','".$sub_row['c_id']."');\n";
					$i=$i+1;
				}   
		echo "}\n";
	}  
	echo "}</script>\n"; 

?>

<html>
<head>
<title>Gender  Drop Down Test</title>
</head>
<body>
I'm a 
<select name='selcategory' id='selcategory' onChange="return getsubcats();">
  <option value='0'></option>

</select>.<br />
<p>
<select name='selsubcat' id='selsubcat' onChange="selectItem(this.value, 'ItemCode')">
  <option value='0'></option>
  
</select>
</p>
</body>
</html>

i hope this helps

I am using PHP/MySQl
And in my Table there are only 3 fields
Id | ItemCode | ItemName

Please guide me.
Thanks
Ayesha

Praveen what is id?
is it my Database Table id?

yes its your database table id

<? 
$cres=mysql_query("SELECT * FROM table  ORDER BY id ");
$ccunt=mysql_num_rows($cres);
	echo "<script language=javascript>\n";
	echo "function getsubcats() {\n";
		echo "var d=document.frmaddpro;\n";
		echo "if(d.selcategory.value==0) {\n";
			echo "d.selsubcat.options.length = 0;\n";
			echo "d.selsubcat.options[0]=new Option(\"Select Subcategory\",\"\")\n";
		echo "}\n";
	while($row_cat=mysql_fetch_array($cres,MYSQL_BOTH)){
		echo "if(d.selcategory.value==".$row_cat['id'].") {\n";
				echo "d.selsubcat.length=0;\n";
				$sub_res=mysql_query("SELECT * FROM table WHERE codeid='".$row_cat['id']."'");  
				$i=1;
				echo "d.selsubcat.options[0]=new Option(\"Select Subcategory \",\"\")\n";
				while($sub_row=mysql_fetch_array($sub_res,MYSQL_BOTH)){
					echo "d.selsubcat .options[".$i."]=new Option('".$sub_row['name']."','".$sub_row['c_id']."');\n";
					$i=$i+1;
				}   
		echo "}\n";
	}  
	echo "}</script>\n"; 

?>

<html>
<head>
<title>Gender  Drop Down Test</title>
</head>
<body>
I'm a 
<select name='selcategory' id='selcategory' onChange="return getsubcats();">
  <option value='0'></option>

</select>.<br />
<p>
<select name='selsubcat' id='selsubcat' onChange="selectItem(this.value, 'ItemCode')">
  <option value='0'></option>
  
</select>
</p>
</body>
</html>

i hope this helps

My table fields are Id,ItemCode and ItemName
Please I am confused
Please

Thanks for your help.

Please give some comments about the code.

Thansk
Ayesha

find the attachment and if you have any other doubts let me know

Why not use ajax? Its better than having php create the javascript which adds to the page size and load time.

How?
Please guide.

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.