473,320 Members | 1,910 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,320 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 18688
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.