0

At the moment, I am trying to create two drop down menus that will act as time intervals. My goal is to set it up where if the user selects a year in the first drop down menu, then the next menu will automatically populate with values that are >= the first menu selection. Here is both drop down menus that gather years by querying to my database.

<p class = 'year'>
<label for="year">
Please select a year
</label>

<select name= 'year'>
echo "<option value= ''> </option>";
<?php
$query = "select distinct year from table";

$result = $conn->query($query);
while($row = $result->fetch_object()) {
  echo "<option value='".$row->year."'>".$row->year."</option>";
 }
?>
</select>
</p>

<p class = 'year'>
<label for="year">
Please select a year
</label>

<select name= 'year2'>
echo "<option value= ''> </option>";
<?php
$query = "select distinct year from table";

$result = $conn->query($query);
while($row = $result->fetch_object()) {
  echo "<option value='".$row->year."'>".$row->year."</option>";
 }
?>
</select>
</p>

Both of these drop down menus gather years from my database. Instead, I want my year2 menu to grab only the years >= the year value that the user selects in the first drop down menu. My question is how do I go about doing this? Do I still have to create three separate files, JQuery file, function file, and this file, or, is there a simpler way to go about this? Any help would be greatly appreciated.

user2562125
  • 179
  • 1
  • 2
  • 10
  • Only way to avoid using jQuery (which would primarily be just to leverage the ajax wrapper), is to submit the form on change, pass that to the file, then show the 2nd dropdown menu. – Ohgodwhy Jul 15 '13 at 01:45

1 Answers1

1

I would try to convert all that logic into a single query that returns all the data you need at once. Then pass it to a JavaScript variable and play around 100% client-side. No need to hammer your DB constantly for something this simple.

Mikhail
  • 8,692
  • 8
  • 56
  • 82
  • I am not familiar with JavaScript variables dealing with chained drop down menus. Do you know where I could get more information on this? – user2562125 Jul 15 '13 at 01:52
  • JS doesn't have anything specifically for dropdowns. Just use arrays or hashed dictionaries and loop through that information using jQuery. Check out this SO: http://stackoverflow.com/questions/815103/jquery-best-practice-to-populate-drop-down – Mikhail Jul 15 '13 at 02:06