I want to use two drop down menus in my php application. All data should select from the database(Mysql) . Eg: 1st drop down(Fruit,Coun try,game) after selecting a item from the first drop down , second one should load perticular data from tha database. If i select Fruit then automatically load Mango,banana.et c to the second drop down. Could you please help me?
Double drop down menue
Collapse
X
-
We are here to help programmers with their PHP problems. And we can only help when we can see and work on the code you have developed.
So show it here and do not forget to enclose any code within the appropriate code tags!
Ronald -
Originally posted by ronverdonkWe are here to help programmers with their PHP problems. And we can only help when we can see and work on the code you have developed.
So show it here and do not forget to enclose any code within the appropriate code tags!
Ronald
This is my first dropdown(Thease are hard coded. no need to select from the database)
[HTML]<select name="name" id="name" >
<option value="Select" selected="selec ted">Select</option>
<option value="Fruit" >Fruit</option>
<option value="Country" >Country</option>
<option value="Game"> Game</option>
</select> [/HTML]
And second drop down should filled depend on the selected value in first dropdown.(get values from mysql database)Comment
-
[code=html]
<!--
----------------------------
here i juz worked in a localhost.
database: demo1;
table: demo_table;
fields(demo_tab le)
item varchar(20);
list varchar(20);
example table data( i here used a simple table structure u can use a well one):
item list
fruit Mango
fruit banana
Country India
Country USA
Country UK
----------------------------
-->
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getItem(sel)
{
var itemCode = sel.options[sel.selectedInd ex].value;
//alert(itemCode) ;
var url= "http://localhost/onchange1.php?i tem="+itemCode;
window.location =url;
}
</script>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td>Item </td>
<td><select id="item" name="item" onchange="getIt em(this)">
<option value="">Select </option>
<option value="Fruit">F ruit</option>
<option value="Country" >Country</option>
</select>
</td>
</tr>
<tr>
<td>List </td>
<td><select id="item_list" name="item_list " >
<option value="">Select </option>
<?php
$i_code=$_REQUE ST['item'];
$con = mysql_connect(" localhost","roo t","");
mysql_select_db ("demo1",$co n);
$query="select list from demo_table where item='$i_code'" ;
$res=mysql_quer y($query);
$num=mysql_num_ rows($res);
for($i=0;$i<$nu m;$i++)
{
$data=mysql_fet ch_array($res);
$v=$data[0].$i;
echo "<option value='$data[0]'>".$data[0]."</option>";
}
?>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
[/code]
it is a simplest method, and also u can use AJAX.Comment
-
Hi sethupnr.
Please use [code] tags when posting your code examples. (See How to ask a question)
[code=ht ml] ...HTML code goes here... [/code]
Thank you.Comment
Comment