944,111 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 18875
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 3rd, 2007
0

PHP mysql drop down populates another drop down

Expand Post »
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

PHP Syntax (Toggle Plain Text)
  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>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danielwoodhead is offline Offline
2 posts
since May 2007
May 4th, 2007
0

Re: PHP mysql drop down populates another drop down

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nsstone is offline Offline
3 posts
since Jan 2006
May 4th, 2007
0

Re: PHP mysql drop down populates another drop down

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danielwoodhead is offline Offline
2 posts
since May 2007
May 4th, 2007
0

Re: PHP mysql drop down populates another drop down

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.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
May 6th, 2007
0

Re: PHP mysql drop down populates another drop down

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.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
May 6th, 2007
0

Re: PHP mysql drop down populates another drop down

Click to Expand / Collapse  Quote originally posted by dr4g ...

AJAX will be the easiest solution to your problem.
AJAX is the only solution
Reputation Points: 18
Solved Threads: 9
Junior Poster
w_3rabi is offline Offline
160 posts
since Dec 2006
May 6th, 2007
0

Re: PHP mysql drop down populates another drop down

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.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
May 7th, 2007
0

Re: PHP mysql drop down populates another drop down

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 ............................
Reputation Points: 18
Solved Threads: 9
Junior Poster
w_3rabi is offline Offline
160 posts
since Dec 2006
May 7th, 2007
0

Re: PHP mysql drop down populates another drop down

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
Reputation Points: 33
Solved Threads: 7
Junior Poster in Training
johny_d is offline Offline
92 posts
since May 2007
May 7th, 2007
0

Re: PHP mysql drop down populates another drop down

ya man
nice technique
but is it as efficient as AJAX
Reputation Points: 18
Solved Threads: 9
Junior Poster
w_3rabi is offline Offline
160 posts
since Dec 2006

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: php+javascript
Next Thread in PHP Forum Timeline: I purchased VBulletin...but





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


Follow us on Twitter


© 2011 DaniWeb® LLC