•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 396,972 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,889 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 965 | Replies: 21
![]() |
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
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. Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
•
•
Join Date: May 2008
Posts: 24
Reputation:
Rep Power: 1
Solved Threads: 0
ok, that is:
php Syntax (Toggle Plain Text)
<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>
Last edited by peter_budo : May 11th, 2008 at 10:18 am. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
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 Syntax (Toggle Plain Text)
<?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>"; ?>
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
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.
If this doesn't work, then, I don't know, something must be wrong from your side. php Syntax (Toggle Plain Text)
<?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>"; ?>
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
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.
You should check this yourself because I dont have the access to your table and I don't know what exactly is the problem. Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
Similar Threads
- php mysql drop down list (PHP)
- drop down list question. (ASP.NET)
- Populate One Drop Down List From Another (ASP.NET)
- Populating a Drop-down List (PHP)
- view employee info for the employee selected from the drop down list (PHP)
- Data Binding to a Drop Down List? (ASP.NET)
- Passing a drop down list item's value (HTML and CSS)
- Blank drop list (PHP)
Other Threads in the PHP Forum
- Previous Thread: Refresh + Combo Box
- Next Thread: need php codings for my task!



Linear Mode