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.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
Can you post your code here ?
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
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>";
?>
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
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>";
?>
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
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.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
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 ?
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
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.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
Use firefox to execute your scripts. Make use of error console to see any javascript error.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
What second page ? Why dont you keep everything in the same page ? :-/
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356