954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP mysql drop down populates another drop down

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
include 'config.php';
include 'opendb.php';
$sql="SELECT event_Name FROM eventdata";
$result=mysql_query($sql);
$optionsevent="";
while ($row=mysql_fetch_array($result)) {
    $event_Name=$row["event_Name"];
    $event_Name=$row["event_Name"];
    $optionsevent.="<OPTION VALUE=\"$event_Name\">".$event_Name;
}
function Generate(){
$sql="SELECT team_ID FROM race WHERE race_Name=$optionsevent";
$result=mysql_query($sql);
$optionsid="";
while ($row=mysql_fetch_array($result)) {
    $team_ID=$row["team_ID"];
    $team_ID=$row["team_ID"];
    $optionsid.="<OPTION VALUE=\"$team_ID\">".$team_ID;
}
}
?>
<html>
<head>
<title>Rally Database - Results Entry Form</title><style type="text/css">
<!--
.style1 {
 font-family: Arial, Helvetica, sans-serif;
 font-size: 24px;
}
body {
 background-color: #CCCCCC;
}
-->
</style><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body>
<div align="center">
  <p><strong><span class="style1">Results Entry Form</span></strong></p>
  <p align="left">Please enter select the Race ID and then the Team ID. </p>
</div>
<form method="post" action="enterresultsinsert.php">
  <label></label>
  <p>
    <label></label>
    <label></label>
    <label>Race ID
    <select name="raceid">
    <OPTION VALUE=0 onChange="Generate();" >Choose
    <?=$optionsevent?>
    </select>
    </select>
    </label>
    <label>Team ID
    <select name="teamid">
    <OPTION VALUE=0>Choose
  <?=$optionsid?>
    </select>
    </select>
    </label>
  </p>
  <p>
    <label>Stage 1 Time
    <input name="stage1" type="text">
    </label>
  </p>
  <p>
    <label>Stage 2 Time
    <input name="stage2" type="text">
    </label>
</p>
  <p>
    <label>Stage 3 Time
    <input name="stage3" type="text">
    </label>
</p>
  <label>Stage 4 Time
  <input name="stage4" type="text">
  </label>
  <p>
    <label>Stage 5 Time
    <input name="stage5" type="text">
    </label>
</p>
  <p>
    <label>Stage 6 Time
    <input name="stage6" type="text">
    </label>
</p>
  <p>
    <label>Stage 7 Time
    <input name="stage7" type="text">
    </label>
</p>
  <p>
    <label>Stage 8 Time
    <input name="stage8" type="text">
    </label>
</p>
  <p>
    <label>Total Time
    <input name="total" type="text">
    </label>
</p>
  <p>
    <label></label>
    <label></label><label>
    <input type="submit" name="Submit" value="Add Results">
    </label>
  </p>
  </form>
</body>
</html>
danielwoodhead
Newbie Poster
2 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

Do you want a server-side refresh of the second SELECT object, or a "

nsstone
Newbie Poster
3 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

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

danielwoodhead
Newbie Poster
2 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Rhyan
Posting Whiz in Training
240 posts since Oct 2006
Reputation Points: 21
Solved Threads: 26
 

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.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 
AJAX will be the easiest solution to your problem.


AJAX is the only solution

w_3rabi
Junior Poster
160 posts since Dec 2006
Reputation Points: 18
Solved Threads: 9
 

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.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

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 ............................

w_3rabi
Junior Poster
160 posts since Dec 2006
Reputation Points: 18
Solved Threads: 9
 

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 :)

johny_d
Junior Poster in Training
94 posts since May 2007
Reputation Points: 33
Solved Threads: 7
 

ya man
nice technique
but is it as efficient as AJAX

w_3rabi
Junior Poster
160 posts since Dec 2006
Reputation Points: 18
Solved Threads: 9
 

I think it depends on how often the information that populates the drop-downs changes. If it doesn't change often (once a week, a month etc...), than the javascript may actually work faster and better for both user and server, because once the js file is cache, there is no more loading on the server.
If the data for the drop-downs changes regularly, than ajax is probably the best option; anyway, it seems there are some browsers that don't support ajax yet, so... some of the users may have problems using that form.

johny_d
Junior Poster in Training
94 posts since May 2007
Reputation Points: 33
Solved Threads: 7
 
anyway, it seems there are some browsers that don't support ajax yet, so... some of the users may have problems using that form.

well these browsers aren't widely used
i mean who uses a textual browser anymore
and about the javascript there still the javascript disabled problem

w_3rabi
Junior Poster
160 posts since Dec 2006
Reputation Points: 18
Solved Threads: 9
 

johnny_d. That was my other technique for the non-AJAX solution. Great minds think alike :)

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 
johny_d
Junior Poster in Training
94 posts since May 2007
Reputation Points: 33
Solved Threads: 7
 

>>nice technique
>>but is it as efficient as AJAX

That was "solution B" in my first reply. It loads the nested Option lists once, so if the user is fiddling around and getting a lot of hits, the "load once" solution might be more efficient.

Google EasySelect for a nice preload solution.

AJAX is sexy but should be used only if data at the server is changing between hits. Otherwise preloading your options or alternative page layers is often more efficient and certainly hits the server fewer times.

nsstone

nsstone
Newbie Poster
3 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

Yep. AJAX would be more dynamic and professional.

Loading everything on the fly intially, would be less server-intensive.

Either will do :)

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 
johny_d
Junior Poster in Training
94 posts since May 2007
Reputation Points: 33
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You