User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: May 2008
Posts: 24
Reputation: almualim is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
almualim almualim is offline Offline
Newbie Poster

please help me in drop down list

  #1  
May 7th, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: please help me in drop down list

  #2  
May 7th, 2008
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*
Reply With Quote  
Join Date: May 2008
Posts: 24
Reputation: almualim is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
almualim almualim is offline Offline
Newbie Poster

Re: please help me in drop down list

  #3  
May 7th, 2008
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 9:59 am.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: please help me in drop down list

  #4  
May 7th, 2008
Can you post your code here ?
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*
Reply With Quote  
Join Date: May 2008
Posts: 24
Reputation: almualim is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
almualim almualim is offline Offline
Newbie Poster

Re: please help me in drop down list

  #5  
May 7th, 2008
ok, that is:

  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 10:18 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: please help me in drop down list

  #6  
May 7th, 2008
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 !)
  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. ?>
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*
Reply With Quote  
Join Date: May 2008
Posts: 24
Reputation: almualim is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
almualim almualim is offline Offline
Newbie Poster

Re: please help me in drop down list

  #7  
May 7th, 2008
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 ';'
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: please help me in drop down list

  #8  
May 7th, 2008
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.
  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. ?>
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*
Reply With Quote  
Join Date: May 2008
Posts: 24
Reputation: almualim is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
almualim almualim is offline Offline
Newbie Poster

Re: please help me in drop down list

  #9  
May 7th, 2008
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...
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: please help me in drop down list

  #10  
May 8th, 2008
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.
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*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 8:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC