hi friends,
i want to display data from mySQL database, and iam using two drop down list, displaying data in second drop down list depending on the selecting from first drop down list, for example:
first drop down list contains states, and second drop down list contains cities.

This has been discussed so many times. Either use ajax or this way. In the first dropdown list, list all the states. Have an onchange event to submit the form for the 1st dropdown list. When a user selects an option from the 1st dropdown list, onchange event will be fired and the form will be submitted. You can then access the selected value of 1st dropdown list by doing $firstdropdownlistvalue = $_POST['dropdownlistname']; You can then query the database for this particular state and get all the cities in the 2nd dropdown list.

i tried that, but the cities didn't appeared in the second dropdown list.
note:
" the code in the same page ".

Can you post your code here ?

ok, that is:

<html >
<head>
<title> my page </title>
</head>
<body >
<?php
            mysql_connect ("localhost","root","");
            mysql_select_db ("University");
            $get_list_result = mysql_query("select Col_Name from College");

 echo '<form name="delete" method="POST" action="delete_dept2.php">';

echo'<select name="col_name">';
while ($recs = mysql_fetch_array($get_list_result)){
            $display_list =  $recs['Col_Name'];
           echo '<option>'; echo ($display_list); echo '</option>';
           }
echo '</select>';
$firstdropdownlistvalue = $_POST['col_name'];

$get_list_result2 = mysql_query("select Col_No from College where Col_Name='$firstdropdownlistvalue'");
$get_list_result3=  mysql_query("select D_Name from Department where Col_No='$get_list_result2'");

echo'<select name="dept_name">';
while ($recs2 = mysql_fetch_array($get_list_result3)){
            $display_list2 =  $recs2['D_Name'];
           echo '<option>'; echo ($display_list2); echo '</option>';
           }
echo '</select>';
?>
</form>
</body>
</html>

First of all, you don't have an onchange event to submit the page. secondly, you should have a value for the option. Here is a simple example. I haven't tested it, but it should work with little tuning (which you have to figure it yourself !)

<?php
	$con = mysql_connect("localhost","root");
	mysql_select_db("test");
	$query = "select col1 from table";
	$result = mysql_query($query);
	echo "<form method='post' action='test.php'>";
	echo "<select name=dropdown1 onchange='javascript: document.form.submit();'>";
	while($row = mysql_fetch_array($result)) {
		$value = $row['col1'];
		echo "<option value='".$value."'>$value</option>";
	}
	echo "</select>";
	
	$firstdropdownlistvalue = $_POST['dropdown1'];
	$query2 = "select * from table where col2 = '$firstdropdownlistvalue'";
	$result2 = mysql_query($query2);
	echo "<select name=dropdown2>";
	while($row2 = mysql_fetch_array($result2)) {
		$value2 = $row2['col1'];
		echo "<option value='".$value2."'>$value2</option>";
	}
	echo "</select>";
	echo "</form>";
?>

i tried that but i get one error in this line:
echo "<select name=dropdown1 onchange='javascript: document.form.submit();'>";

the error is:
parse error, unexpected T_STRING, expecting ',' or ';'

Okay ! Well, I just re-wrote the whole code and it works ! Change the column names and tablename to suit your needs ! :) If this doesn't work, then, I don't know, something must be wrong from your side.

<?php
	$con = mysql_connect("localhost","root");
	mysql_select_db("test");
	
	$firstdropdownlistvalue = $_POST['dropdown1'];
	
	$query = "select order_id from orders";
	$result = mysql_query($query);
	echo "<form method='post' name='form1' action='test.php'>";
	echo "<select name=dropdown1 onchange='javascript: document.form1.submit();'>";
	while($row = mysql_fetch_array($result)) {
		$value = $row['order_id'];
		if($value == $firstdropdownlistvalue) { $selected = "selected"; } else { $selected = ""; }
		echo "<option value='".$value."' $selected>".$value."</option>";
	}
	echo "</select>";
	
	$query2 = "select * from orders where order_id = '$firstdropdownlistvalue'";
	$result2 = mysql_query($query2);
	echo "<select name=dropdown2>";
	while($row2 = mysql_fetch_array($result2)) {
		$value2 = $row2['amount'];
		echo "<option value='".$value2."'>$value2</option>";
	}
	echo "</select>";
	echo "</form>";
?>

thank you very much, the error is solved but still data dosn't appear in the second dropdown list, I appreciate your Effort, please if you know the solution for this problem tell me, and iam Wait you.
thanks...

Hi, the above code works fine. You just have to change the tablenames/column names. Anyway, When the page gets submitted, echo the value of 1st dropdownlist. See if the value is passed correctly. If yes, then the problem must be with your 2nd query. Check your query. See if your query is right. If it is correct, then, something is going wrong while fetching the records from the table. :) You should check this yourself because I dont have the access to your table and I don't know what exactly is the problem.

hi, ok i tried to print this value: $firstdropdownlistvalue = $_POST;
by echo($firstdropdownlistvalue);
but it's doesn't appeared in the browser. why?

Post your latest code.

okay:

<?php
            mysql_connect ("localhost","root","");
            mysql_select_db ("University");
             
            $firstdropdownlistvalue = $_POST['col_name'];

            $get_list_result = mysql_query("select Col_Name from College");

 echo '<form name="delete" method="POST" action="delete_dept2.php">';

echo'<select name=col_name onchange=javascript: document.delete.submit(); style="position:absolute;left:311px;top:146px;width:303px;z-index:2">';
while ($recs = mysql_fetch_array($get_list_result)){
           
            $display_list =  $recs['Col_Name'];
if($display_list==$firstdropdownlistvalue){
 $selected = "selected";
}
else
{ $selected = ""; 
}     
echo "<option value='".$display_list."' $selected>" .$display_list."</option>";
           }
echo "</select>";
echo ($firstdropdownlistvalue);

$get_list_result2 = mysql_query("select * from College where Col_Name='$firstdropdownlistvalue'");
$get_list_result3=  mysql_query("select D_Name from Department where Col_No='$get_list_result2'");

echo'<select name="formselect2" style="position:absolute;left:310px;top:223px;width:308px;z-index:4">';
while ($recs2 = mysql_fetch_array($get_list_result3)){
           
            $display_list2 =  $recs2['D_Name'];
     
           echo "<option value='".$display_list2."'> $display_list2 </option>";
           }
echo '</select>';
?>

onchange="javascript: document.delete.submit();" instead of
onchange=javascript: document.delete.submit();

Secondly,

$get_list_result2 = mysql_query("select * from College where Col_Name='$firstdropdownlistvalue'");
$get_list_result3= mysql_query("select D_Name from Department where Col_No='$get_list_result2'");

is wrong. What are you trying to do ?

see the last update on the code:

<?php
            mysql_connect ("localhost","root","");
            mysql_select_db ("University");
             
            $firstdropdownlistvalue = $_POST['col_name'];

            $get_list_result = mysql_query("select Col_Name from College");

 echo '<form name="delete" method="POST" action="delete_dept2.php">';

echo'<select name=col_name onchange="javascript: document.delete.submit();" style="position:absolute;left:311px;top:146px;width:303px;z-index:2">';
while ($recs = mysql_fetch_array($get_list_result)){
           
            $display_list =  $recs['Col_Name'];
if($display_list==$firstdropdownlistvalue){
 $selected = "selected";
}
else
{ $selected = ""; 
}     
echo "<option value='".$display_list."' $selected>" .$display_list."</option>";
           }
echo "</select>";
echo ($firstdropdownlistvalue);

$get_list_result2 = mysql_query("select col_No from College where Col_Name='$firstdropdownlistvalue'");
while ($recs2 = mysql_fetch_array($get_list_result2)){
           
            $display_list2 =  $recs2['col_No'];
}



$get_list_result3=  mysql_query("select D_Name from Department where Col_No='$$display_list2'");

echo'<select name="formselect2" style="position:absolute;left:310px;top:223px;width:308px;z-index:4">';
while ($recs3 = mysql_fetch_array($get_list_result3)){
           
            $display_list3 =  $recs3['D_Name'];
     
           echo "<option value='".$display_list3."'> $display_list3 </option>";
           }
echo '</select>';
?>
$get_list_result2 = mysql_query("select col_No from College where Col_Name='$firstdropdownlistvalue'");

here i want to get college_no(col_No) from college table, if the college name(col_Name) equals the value came from 1st dropdown list($firstdropdownlistvalue) $get_list_result3= mysql_query("select D_Name from Department where col_No='$display_list2'"); and here i want to get department name(D_Name) from Department table, if college_no(col_No) equals $display_list2

Then use a sub query.

$query = "select D_Name from Department where col_No = (select col_No from College where col_name = '$firstdropdownlistvalue')";

This will return the D_name of the selected col_name.

Tired with me, but the problem still exist and I do not know what to do...

Use firefox to execute your scripts. Make use of error console to see any javascript error.

i tried with firefox, but when i was select value from 1st dropdown list Transmission occurs for the second page, but i want the values appear in the same page.

What second page ? Why dont you keep everything in the same page ? :-/

i mean two dropdown lists in same page

Yep.. My example works with 2 dropdowns in a same page.

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.