943,789 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2941
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 7th, 2008
0

please help me in drop down list

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
almualim is offline Offline
28 posts
since May 2008
May 7th, 2008
0

Re: please help me in drop down list

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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
May 7th, 2008
0

Re: please help me in drop down list

i tried that, but the cities didn't appeared in the second dropdown list.
note:
" the code in the same page ".
Last edited by almualim; May 7th, 2008 at 10:59 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
almualim is offline Offline
28 posts
since May 2008
May 7th, 2008
0

Re: please help me in drop down list

Can you post your code here ?
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
May 7th, 2008
0

Re: please help me in drop down list

ok, that is:

php Syntax (Toggle Plain Text)
  1. <html >
  2. <head>
  3. <title> my page </title>
  4. </head>
  5. <body >
  6. <?php
  7. mysql_connect ("localhost","root","");
  8. mysql_select_db ("University");
  9. $get_list_result = mysql_query("select Col_Name from College");
  10.  
  11. echo '<form name="delete" method="POST" action="delete_dept2.php">';
  12.  
  13. echo'<select name="col_name">';
  14. while ($recs = mysql_fetch_array($get_list_result)){
  15. $display_list = $recs['Col_Name'];
  16. echo '<option>'; echo ($display_list); echo '</option>';
  17. }
  18. echo '</select>';
  19. $firstdropdownlistvalue = $_POST['col_name'];
  20.  
  21. $get_list_result2 = mysql_query("select Col_No from College where Col_Name='$firstdropdownlistvalue'");
  22. $get_list_result3= mysql_query("select D_Name from Department where Col_No='$get_list_result2'");
  23.  
  24. echo'<select name="dept_name">';
  25. while ($recs2 = mysql_fetch_array($get_list_result3)){
  26. $display_list2 = $recs2['D_Name'];
  27. echo '<option>'; echo ($display_list2); echo '</option>';
  28. }
  29. echo '</select>';
  30. ?>
  31. </form>
  32. </body>
  33. </html>
Last edited by peter_budo; May 11th, 2008 at 11:18 am. Reason: Keep It Organized - please use [code] tags
Reputation Points: 10
Solved Threads: 0
Light Poster
almualim is offline Offline
28 posts
since May 2008
May 7th, 2008
0

Re: please help me in drop down list

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)
  1. <?php
  2. $con = mysql_connect("localhost","root");
  3. mysql_select_db("test");
  4. $query = "select col1 from table";
  5. $result = mysql_query($query);
  6. echo "<form method='post' action='test.php'>";
  7. echo "<select name=dropdown1 onchange='javascript: document.form.submit();'>";
  8. while($row = mysql_fetch_array($result)) {
  9. $value = $row['col1'];
  10. echo "<option value='".$value."'>$value</option>";
  11. }
  12. echo "</select>";
  13.  
  14. $firstdropdownlistvalue = $_POST['dropdown1'];
  15. $query2 = "select * from table where col2 = '$firstdropdownlistvalue'";
  16. $result2 = mysql_query($query2);
  17. echo "<select name=dropdown2>";
  18. while($row2 = mysql_fetch_array($result2)) {
  19. $value2 = $row2['col1'];
  20. echo "<option value='".$value2."'>$value2</option>";
  21. }
  22. echo "</select>";
  23. echo "</form>";
  24. ?>
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
May 7th, 2008
0

Re: please help me in drop down list

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 ';'
Reputation Points: 10
Solved Threads: 0
Light Poster
almualim is offline Offline
28 posts
since May 2008
May 7th, 2008
0

Re: please help me in drop down list

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 Syntax (Toggle Plain Text)
  1. <?php
  2. $con = mysql_connect("localhost","root");
  3. mysql_select_db("test");
  4.  
  5. $firstdropdownlistvalue = $_POST['dropdown1'];
  6.  
  7. $query = "select order_id from orders";
  8. $result = mysql_query($query);
  9. echo "<form method='post' name='form1' action='test.php'>";
  10. echo "<select name=dropdown1 onchange='javascript: document.form1.submit();'>";
  11. while($row = mysql_fetch_array($result)) {
  12. $value = $row['order_id'];
  13. if($value == $firstdropdownlistvalue) { $selected = "selected"; } else { $selected = ""; }
  14. echo "<option value='".$value."' $selected>".$value."</option>";
  15. }
  16. echo "</select>";
  17.  
  18. $query2 = "select * from orders where order_id = '$firstdropdownlistvalue'";
  19. $result2 = mysql_query($query2);
  20. echo "<select name=dropdown2>";
  21. while($row2 = mysql_fetch_array($result2)) {
  22. $value2 = $row2['amount'];
  23. echo "<option value='".$value2."'>$value2</option>";
  24. }
  25. echo "</select>";
  26. echo "</form>";
  27. ?>
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
May 7th, 2008
0

Re: please help me in drop down list

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...
Reputation Points: 10
Solved Threads: 0
Light Poster
almualim is offline Offline
28 posts
since May 2008
May 8th, 2008
0

Re: please help me in drop down list

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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Refresh + Combo Box
Next Thread in PHP Forum Timeline: need php codings for my task!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC