| | |
please help me in drop down list
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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. Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: May 2008
Posts: 28
Reputation:
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 11:18 am. Reason: Keep It Organized - please use [code] tags
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>"; ?>
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
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>"; ?>
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
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. Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
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!
| Thread Tools | Search this Thread |
.htaccess alerts apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include insert ip javasciptvalidation javascript joomla keywords limit link login mail matching menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect remote script search securephp server sessions shot sms source space sql subscription syntax system table tutorial tutorials update upload url validator variable video web youtube






