PHP mysql drop down populates another drop down

Reply

Join Date: May 2007
Posts: 2
Reputation: danielwoodhead is an unknown quantity at this point 
Solved Threads: 0
danielwoodhead danielwoodhead is offline Offline
Newbie Poster

PHP mysql drop down populates another drop down

 
0
  #1
May 3rd, 2007
Hi

I am tring to populate a drop down by selecting another drop down.
The first drop dwon populates from eventdata table and i want the second drop down to populate from a race table. I have tried to find the row which contains the same event id as the first drop down box and then display the team id which has the same event id.

NOTE event id and race id will have the same data in them, bad naming on my part

  1. <?php
  2. include 'config.php';
  3. include 'opendb.php';
  4. $sql="SELECT event_Name FROM eventdata";
  5. $result=mysql_query($sql);
  6. $optionsevent="";
  7. while ($row=mysql_fetch_array($result)) {
  8. $event_Name=$row["event_Name"];
  9. $event_Name=$row["event_Name"];
  10. $optionsevent.="<OPTION VALUE=\"$event_Name\">".$event_Name;
  11. }
  12. function Generate(){
  13. $sql="SELECT team_ID FROM race WHERE race_Name=$optionsevent";
  14. $result=mysql_query($sql);
  15. $optionsid="";
  16. while ($row=mysql_fetch_array($result)) {
  17. $team_ID=$row["team_ID"];
  18. $team_ID=$row["team_ID"];
  19. $optionsid.="<OPTION VALUE=\"$team_ID\">".$team_ID;
  20. }
  21. }
  22. ?>
  23. <html>
  24. <head>
  25. <title>Rally Database - Results Entry Form</title><style type="text/css">
  26. <!--
  27. .style1 {
  28. font-family: Arial, Helvetica, sans-serif;
  29. font-size: 24px;
  30. }
  31. body {
  32. background-color: #CCCCCC;
  33. }
  34. -->
  35. </style><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
  36. <body>
  37. <div align="center">
  38. <p><strong><span class="style1">Results Entry Form</span></strong></p>
  39. <p align="left">Please enter select the Race ID and then the Team ID. </p>
  40. </div>
  41. <form method="post" action="enterresultsinsert.php">
  42. <label></label>
  43. <p>
  44. <label></label>
  45. <label></label>
  46. <label>Race ID
  47. <select name="raceid">
  48. <OPTION VALUE=0 onChange="Generate();" >Choose
  49. <?=$optionsevent?>
  50. </select>
  51. </select>
  52. </label>
  53. <label>Team ID
  54. <select name="teamid">
  55. <OPTION VALUE=0>Choose
  56. <?=$optionsid?>
  57. </select>
  58. </select>
  59. </label>
  60. </p>
  61. <p>
  62. <label>Stage 1 Time
  63. <input name="stage1" type="text">
  64. </label>
  65. </p>
  66. <p>
  67. <label>Stage 2 Time
  68. <input name="stage2" type="text">
  69. </label>
  70. </p>
  71. <p>
  72. <label>Stage 3 Time
  73. <input name="stage3" type="text">
  74. </label>
  75. </p>
  76. <label>Stage 4 Time
  77. <input name="stage4" type="text">
  78. </label>
  79. <p>
  80. <label>Stage 5 Time
  81. <input name="stage5" type="text">
  82. </label>
  83. </p>
  84. <p>
  85. <label>Stage 6 Time
  86. <input name="stage6" type="text">
  87. </label>
  88. </p>
  89. <p>
  90. <label>Stage 7 Time
  91. <input name="stage7" type="text">
  92. </label>
  93. </p>
  94. <p>
  95. <label>Stage 8 Time
  96. <input name="stage8" type="text">
  97. </label>
  98. </p>
  99. <p>
  100. <label>Total Time
  101. <input name="total" type="text">
  102. </label>
  103. </p>
  104. <p>
  105. <label></label>
  106. <label></label><label>
  107. <input type="submit" name="Submit" value="Add Results">
  108. </label>
  109. </p>
  110. </form>
  111. </body>
  112. </html>
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 3
Reputation: nsstone is an unknown quantity at this point 
Solved Threads: 0
nsstone nsstone is offline Offline
Newbie Poster

Re: PHP mysql drop down populates another drop down

 
0
  #2
May 4th, 2007
Do you want a server-side refresh of the second SELECT object, or a "<BODY onload=..." javascript that contains all the nested option lists?

The first solution could use AJAX to rewrite the options, triggered by the onchange event of the first SELECT object.

For the second solution, google the EasySelect javascript off the web, then use php to hit your database and write the arrays that Easyselect uses to populate its SELECT options.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 2
Reputation: danielwoodhead is an unknown quantity at this point 
Solved Threads: 0
danielwoodhead danielwoodhead is offline Offline
Newbie Poster

Re: PHP mysql drop down populates another drop down

 
0
  #3
May 4th, 2007
the first solution appears to be what i want, i think this is what i have tried in the code above, call the generate function when the first one is selected
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: PHP mysql drop down populates another drop down

 
0
  #4
May 4th, 2007
You will have to figure it out using AJAX requests.
Good luck, as it will turn out you have to redesign the code to match the AJAX way.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: PHP mysql drop down populates another drop down

 
0
  #5
May 6th, 2007
Yes, AJAX is your solution.
You're looking to use the onChange() javascript function within your Drop-Down box.

Upon changing the value, you'll need to re-grab the information from the database.

AJAX will be the easiest solution to your problem.
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 160
Reputation: w_3rabi is an unknown quantity at this point 
Solved Threads: 8
w_3rabi's Avatar
w_3rabi w_3rabi is offline Offline
Junior Poster

Re: PHP mysql drop down populates another drop down

 
0
  #6
May 6th, 2007
Originally Posted by dr4g View Post

AJAX will be the easiest solution to your problem.
AJAX is the only solution
programming is an art ,only for those who can understand it.
- th3 php wr3nch -
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: PHP mysql drop down populates another drop down

 
0
  #7
May 6th, 2007
That's extremley narrow minded w_3rabi.
There's a number of solutions available.

I give professional honest advice.
So i'm re-interating, that AJAX is the easiest solution to your problem.
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 160
Reputation: w_3rabi is an unknown quantity at this point 
Solved Threads: 8
w_3rabi's Avatar
w_3rabi w_3rabi is offline Offline
Junior Poster

Re: PHP mysql drop down populates another drop down

 
0
  #8
May 7th, 2007
again i would say
is there any solution that does not posts the page back to the sever and does not make your page weight like a ton
except AJAX.......
well i really would like to know about it
and anyway sorry for using the word "ONLY" after rethinking it is not so professional
word ............................
programming is an art ,only for those who can understand it.
- th3 php wr3nch -
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 68
Reputation: johny_d is an unknown quantity at this point 
Solved Threads: 4
johny_d's Avatar
johny_d johny_d is offline Offline
Junior Poster in Training

Re: PHP mysql drop down populates another drop down

 
0
  #9
May 7th, 2007
There is at least one other solution to the problem: you save all your data in javascript tables and use onchange() event on one drop down to populate another from respective javascript table. The javascript can be put in an external file (if the information doesn't change to often, so it can be cached by the browsers and load faster) or can be generated on the fly from php, every time the script loads, if it updates often.
I've done this for a site few years ago, when ajax wasn't an option. And it works like a charm
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 160
Reputation: w_3rabi is an unknown quantity at this point 
Solved Threads: 8
w_3rabi's Avatar
w_3rabi w_3rabi is offline Offline
Junior Poster

Re: PHP mysql drop down populates another drop down

 
0
  #10
May 7th, 2007
ya man
nice technique
but is it as efficient as AJAX
programming is an art ,only for those who can understand it.
- th3 php wr3nch -
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC