473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to show dependent drop down list in forms?

I'm using two drop down list ina form. I have generated the first drop down list from MySQL database. When i select an option from first drop down list, i have to generate second drop down list options by the selected value of first drop down list from MySQL database.

For example, the first drop down list contains Animals, Birds... If i select the Animal option, the second drop down list should show like Lion, Tiger....

Both lists should be geneted from database only. Any script available for this?
What I have to do? Any one can help me?!
Oct 8 '06 #1
5 18692
raji20
28
YOU ARE URGED TO USE THE code OR php TAGS WHEN POSTING CODE!!!!
[php]
<?php
mysql_connect("hostname","username","password");
mysql_select_db("databasename");
$firstQry = mysql_query("select * from table1") or die(mysql_error()); // table 1

//////After getting the id from the table1 supply that posted id to get the resuls from the table2 //////////////
$secondQry = mysql_query("select * from table2 where edu_id = '".$_POST["first"]."'") or die(mysql_error()); // table 2
?>
<form name="check" method="post">
<!-- First DropDown starts here -->
<select name="first" onchange="this.form.submit()">
<?php while($res = mysql_fetch_array($firstQry)){?>
<option value="<?php echo $res["id"]?>"><?php echo $res["fullname"]?></option>
<?php }unset($firstQry,$res);?>
</select>
<!-- First DropDown ends here -->
<!-- Second DropDown starts here -->
<?php if(isset($_POST["first"])){?>
<select name="second">
<?php while($row = mysql_fetch_array($secondQry)){?>
<option value="<?php echo $row["edu_id "]?>"><?php echo $row["edu_qualification"]?></option>
<?php }
}unset($secondQry,$row);?>
</select>
<!-- Second DropDown ends here -->
</form>
[/php]
YOU ARE URGED TO USE THE code OR php TAGS WHEN POSTING CODE!!!!
Oct 9 '06 #2
coool
67
I've used your code

I'm having some problems with submiting in the first query.. I mean when I submit in the first query and i ask later for value it gives the next submited value in first query and the previous chosen value of second query

how can I use the same code but without submiting inside my first query ?

maybe I need to use Java Script ??
Aug 9 '07 #3
erp23
26
the best way to do this is to use AJAX - there is a similar post in the forums relating to this. I have used this successfully for a number of drop down boxes for exactly the same situation.

you need an html page, a js page and the php page.

in your html drop box one you will need an onchange function linking it to the javascript file.
in the javascript, you should include the xmlhttp request to the php page that queries the database for the drop down list filtered, for example, a URL variable generated from the javascript. The php should have an echo function, and this can be used to populate your second drop box using getelementbyid.innerhtml in the javascript page

the tutorial at w3schools.com for AJAX should get you going on the javascript syntax you will need, especially the one relating to AJAX and database interactions...
Aug 9 '07 #4
the best way to do this is to use AJAX - there is a similar post in the forums relating to this. I have used this successfully for a number of drop down boxes for exactly the same situation.

you need an html page, a js page and the php page.

in your html drop box one you will need an onchange function linking it to the javascript file.
in the javascript, you should include the xmlhttp request to the php page that queries the database for the drop down list filtered, for example, a URL variable generated from the javascript. The php should have an echo function, and this can be used to populate your second drop box using getelementbyid.innerhtml in the javascript page

the tutorial at w3schools.com for AJAX should get you going on the javascript syntax you will need, especially the one relating to AJAX and database interactions...

Try this site....
http://roshanbh.com.np/2008/01/popul...x-and-php.html
Mar 10 '08 #5
I am using this code and am able to get it to work with my database for the first two option. I have soccer data, and the first dropdown I select the league, then based on that, the second dropdown populates with teams based on what league they are in; this is to select the home team. I am trying to add a 3rd dropdown which also populates with teams based on the league, which would be the away team.

I have two issues:

1.) The 3rd dropdown shows up after I select from the 2nd dropdown, but is not populated
2.) When I do the last "submit", I want to have it spit out some information based on the teams selected. I have the original script running perfectly, but of course the dropdowns are not populated dynamically; they list *all* the teams, and I press the "Submit" button only once. How would I tell the script after I choose from the 3rd dropdown to actually submit the data and spit out my results?

Here is my code as-is (just the three dropdowns; nothing extra yet):

Expand|Select|Wrap|Line Numbers
  1.  
  2. ?php
  3. require_once("../_includes/db_con.inc.php");
  4. $database="soccerData";
  5. $firstQry = mysql_query("SELECT DISTINCT League FROM $database ORDER BY League") or die(mysql_error());
  6. $secondQry = mysql_query("SELECT DISTINCT HomeTeam FROM $database WHERE League = '".$_POST["first"]."'") or die(mysql_error());
  7. $thirdQry = mysql_query("SELECT DISTINCT AwayTeam FROM $database WHERE League = '".$_POST["first"]."'") or die(mysql_error());
  8. ?>
  9.  
  10. <form name="check" method="post">
  11. <!-- First DropDown starts here -->
  12. <select name="first" onchange="this.form.submit()">
  13. <?php
  14. echo "<option value=\"null\">Select League</option>";
  15. while($row1 = mysql_fetch_array($firstQry)) { ?>
  16. <option value="<?php echo $row1['League'] ?>"><?php echo $row1['League'] ?></option>
  17. <?php } unset($firstQry,$row1); ?>
  18. </select>
  19. <!-- First DropDown ends here -->
  20. <!-- Second DropDown starts here -->
  21. <?php if(isset($_POST['first'])) { ?>
  22. <select name="second" onchange="this.form.submit()">
  23. <?php
  24. echo "<option value=\"null\">Select Home Team</option>";
  25. while($row2 = mysql_fetch_array($secondQry)) { ?>
  26. <option value="<?php echo $row2['HomeTeam'] ?>"><?php echo $row2['HomeTeam'] ?></option>
  27. <?php }
  28. } unset($secondQry,$row2); ?>
  29. </select>
  30. <!-- Second DropDown ends here -->
  31.  
  32. !-- Third DropDown starts here -->
  33. <?php if(isset($_POST['second'])) { ?>
  34. <select name="third">
  35. <?php
  36. echo "<option value=\"null\">Select Away Team</option>";
  37. while($row3 = mysql_fetch_array($thirdQry)) { ?>
  38. <option value="<?php echo $row3['AwayTeam'] ?>"><?php echo $row3['AwayTeam'] ?></option>
  39. <?php }
  40. } unset($thirdQry,$row3); ?>
  41. </select>
  42. <!-- Third DropDown ends here -->
  43. <input type="submit" value="submit" name="submit">
  44. </form>
  45.  
Dec 6 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Dec | last post by:
Ok to simplify things I'll just give an example. This is pretty much what I want to do (minus the postcode): http://www.perrys.co.uk/usedcar?ID=F5J9BNNBMVK00DF I have relatively little...
1
by: Dec | last post by:
Ok to simplify things I'll just give an example. This is pretty much what I want to do (minus the postcode): http://www.perrys.co.uk/usedcar?ID=F5J9BNNBMVK00DF I have relatively little...
2
by: SamSpade | last post by:
There seems to be two ways to put things on the clipboard ( I don't mean different formats): SetClipboardData and OleSetClipboard If I want to get data off the clipboard do I care how it was put...
3
by: Ajay Krishnan Thampi | last post by:
I have a slight problem implementing 'drag and drop' from a datagrid to a tree-view. I have pasted my code below. Someone please advice me on what to do...pretty blur right now. ==code== ...
3
by: Don Wash | last post by:
Hi There! I have a Server-side Drop-down box in ASP.NET (VB) page. What do I do to widen the Drop down box's Pull-Down list's width? I'm not talking about the Drop-down box's width but the box...
10
by: lorirobn | last post by:
Hi, I have a form with several combo boxes, continuous form format, with record source a query off an Item Table. The fields are Category, Subcategory, and Color. I am displaying descriptions,...
1
by: ashok893 | last post by:
I'm using two drop down list in a form. I have generated the first drop down list from MySQL database with the help of PHP. When i select an option from first drop down list, i have to generate...
2
by: RAM | last post by:
I need to have two drop down lists in a edited row of a data list. One drop down list should contain Groups and the second one should contain Materials from *selected* group. Thus, I have written:...
1
by: inbamca | last post by:
Hi, Functionality is like when pressing 'CTRL+click' on drop down list in the main jsp page, a popup window appears with a list box contains all the values of the drop down clicked on the main page....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.