Auto-populate HTML Drop Down Menus
- mbenken
- Born


- Joined: Feb 24, 2011
- Posts: 2
- Status: Offline
Hello,
I have something to ponder for everyone. I have drop-down "A" which is a list of months and drop-down "B" with is a list of 5 years, the years being the current year minus one through plus four. There two more drop-downs "C" and "D": "C" has a list of months and "D" has a list of 5 years. I was wondering if there was a way of choosing a month in drop-down "A" and year in drop-down "B" and have "C" and "D" be auto-populated with a month and year that is 23 months in the future. For Example, I choose Mar for "A" and 2011 for "B". Can "C" and "D" get auto-populated with Feb and 2013 respectively? Can this be done? If so, would I use JavaScript or PHP considering the code I already have?
Here is my code so far:
Thanks,
Michael
I have something to ponder for everyone. I have drop-down "A" which is a list of months and drop-down "B" with is a list of 5 years, the years being the current year minus one through plus four. There two more drop-downs "C" and "D": "C" has a list of months and "D" has a list of 5 years. I was wondering if there was a way of choosing a month in drop-down "A" and year in drop-down "B" and have "C" and "D" be auto-populated with a month and year that is 23 months in the future. For Example, I choose Mar for "A" and 2011 for "B". Can "C" and "D" get auto-populated with Feb and 2013 respectively? Can this be done? If so, would I use JavaScript or PHP considering the code I already have?
Here is my code so far:
Code: [ Select ]
[php]<span style="margin-right:3px;">Date Range (max range: 24 months)</span><br />
<select name="from_mo">
<option></option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<select name="from_yr">
<option></option>
<?php
$year = date("Y");
$i = $year-1;
while ($i < $year+4) {
echo '<option>'.$i.'</option>';
$i++;
}
?>
</select>
<span style="margin-left:3px; margin-right:3px;"> to </span>
<select name="to_mo">
<option></option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<select name="to_yr">
<option></option>
<?php
$year = date("Y");
$i = $year-1;
while ($i < $year+4) {
'<option>'.$i.'</option>';
$i++;
}
?>
</select>[/php]
<select name="from_mo">
<option></option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<select name="from_yr">
<option></option>
<?php
$year = date("Y");
$i = $year-1;
while ($i < $year+4) {
echo '<option>'.$i.'</option>';
$i++;
}
?>
</select>
<span style="margin-left:3px; margin-right:3px;"> to </span>
<select name="to_mo">
<option></option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<select name="to_yr">
<option></option>
<?php
$year = date("Y");
$i = $year-1;
while ($i < $year+4) {
'<option>'.$i.'</option>';
$i++;
}
?>
</select>[/php]
- [php]<span style="margin-right:3px;">Date Range (max range: 24 months)</span><br />
- <select name="from_mo">
- <option></option>
- <option>Jan</option>
- <option>Feb</option>
- <option>Mar</option>
- <option>Apr</option>
- <option>May</option>
- <option>Jun</option>
- <option>Jul</option>
- <option>Aug</option>
- <option>Sep</option>
- <option>Oct</option>
- <option>Nov</option>
- <option>Dec</option>
- </select>
- <select name="from_yr">
- <option></option>
- <?php
- $year = date("Y");
- $i = $year-1;
- while ($i < $year+4) {
- echo '<option>'.$i.'</option>';
- $i++;
- }
- ?>
- </select>
- <span style="margin-left:3px; margin-right:3px;"> to </span>
- <select name="to_mo">
- <option></option>
- <option>Jan</option>
- <option>Feb</option>
- <option>Mar</option>
- <option>Apr</option>
- <option>May</option>
- <option>Jun</option>
- <option>Jul</option>
- <option>Aug</option>
- <option>Sep</option>
- <option>Oct</option>
- <option>Nov</option>
- <option>Dec</option>
- </select>
- <select name="to_yr">
- <option></option>
- <?php
- $year = date("Y");
- $i = $year-1;
- while ($i < $year+4) {
- '<option>'.$i.'</option>';
- $i++;
- }
- ?>
- </select>[/php]
Thanks,
Michael
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
February 24th, 2011, 8:41 am
- Bigwebmaster
- Site Admin


- Joined: Dec 20, 2002
- Posts: 8922
- Loc: Seattle, WA & Phoenix, AZ
- Status: Offline
I am curious if you ever found out a solution for this. I would say this can be done, and in my opinion probably easiest using a bit of PHP and Javascript together. If I were doing it I would use PHP to populate everything based on today's date according to your server, and then write some sort of Javascript to automatically change fields depending on what is chosen.
Ozzu Hosting - Want your website on a fast server like Ozzu?
- mbenken
- Born


- Joined: Feb 24, 2011
- Posts: 2
- Status: Offline
Actually, I did discover a solution. After the first 'month' and 'year' are set in the first set of drop-downs, the page is re-loaded placing a month and year in second set of drop-downs. There is an onChange event in the first 'year' drop-down. The re-loaded page has the chosen month and year in the URL. I use the $_GET method the obtain the two values, do a series of PHP if/then statements for the second month, and PHP calculations for the second year. Then I put the second month and year values in respective values of the second set of drop-downs.
Code: [ Select ]
<?php
if (isset($_GET['m'])) {
if ($_GET['m'] == "Jan") {
$monthrangeend = "Dec";
}
if ($_GET['m'] == "Feb") {
$monthrangeend = "Jan";
}
if ($_GET['m'] == "Mar") {
$monthrangeend = "Feb";
}
if ($_GET['m'] == "Apr") {
$monthrangeend = "Mar";
}
if ($_GET['m'] == "May") {
$monthrangeend = "Apr";
}
if ($_GET['m'] == "Jun") {
$monthrangeend = "May";
}
if ($_GET['m'] == "Jul") {
$monthrangeend = "Jun";
}
if ($_GET['m'] == "Aug") {
$monthrangeend = "Jul";
}
if ($_GET['m'] == "Sep") {
$monthrangeend = "Aug";
}
if ($_GET['m'] == "Oct") {
$monthrangeend = "Sep";
}
if ($_GET['m'] == "Nov") {
$monthrangeend = "Oct";
}
if ($_GET['m'] == "Dec") {
$monthrangeend = "Nev";
}
} else {
$monthrangeend = "";
}
if (isset($_GET['y'])) {
$yearplustwo = $_GET['y'] + 2;
} else {
$yearplustwo = "";
}
?>
<form name="your form name" method="get" action="some action here">
<span style="margin-right:3px;">Date Range (max range: 24 months)</span><br />
<select name="from_mo" id="frommo">
<option><?php echo $_GET["m"];?></option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<select name="from_yr" id="fromyr" onchange="window.location.href='/members/index.php?m=' + this.form.frommo.options[this.form.frommo.selectedIndex].value + '&y=' + this.form.fromyr.options[this.form.fromyr.selectedIndex].value">
<option><?php echo $_GET["y"];?></option>
<?php
$year = date("Y");
$i = $year-1;
while ($i < $year+4) {
echo '<option>'.$i.'</option>';
$i++;
}
?>
</select>
<span style="margin-left:3px; margin-right:3px;"> to </span>
<select name="to_mo">
<option><?php echo $monthrangeend;?></option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<select name="to_yr">
<option><?php echo $yearplustwo;?></option>
<?php
$year = date("Y");
$i = $year-1;
while ($i < $year+4) {
echo '<option>'.$i.'</option>';
$i++;
}
?>
</select>
if (isset($_GET['m'])) {
if ($_GET['m'] == "Jan") {
$monthrangeend = "Dec";
}
if ($_GET['m'] == "Feb") {
$monthrangeend = "Jan";
}
if ($_GET['m'] == "Mar") {
$monthrangeend = "Feb";
}
if ($_GET['m'] == "Apr") {
$monthrangeend = "Mar";
}
if ($_GET['m'] == "May") {
$monthrangeend = "Apr";
}
if ($_GET['m'] == "Jun") {
$monthrangeend = "May";
}
if ($_GET['m'] == "Jul") {
$monthrangeend = "Jun";
}
if ($_GET['m'] == "Aug") {
$monthrangeend = "Jul";
}
if ($_GET['m'] == "Sep") {
$monthrangeend = "Aug";
}
if ($_GET['m'] == "Oct") {
$monthrangeend = "Sep";
}
if ($_GET['m'] == "Nov") {
$monthrangeend = "Oct";
}
if ($_GET['m'] == "Dec") {
$monthrangeend = "Nev";
}
} else {
$monthrangeend = "";
}
if (isset($_GET['y'])) {
$yearplustwo = $_GET['y'] + 2;
} else {
$yearplustwo = "";
}
?>
<form name="your form name" method="get" action="some action here">
<span style="margin-right:3px;">Date Range (max range: 24 months)</span><br />
<select name="from_mo" id="frommo">
<option><?php echo $_GET["m"];?></option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<select name="from_yr" id="fromyr" onchange="window.location.href='/members/index.php?m=' + this.form.frommo.options[this.form.frommo.selectedIndex].value + '&y=' + this.form.fromyr.options[this.form.fromyr.selectedIndex].value">
<option><?php echo $_GET["y"];?></option>
<?php
$year = date("Y");
$i = $year-1;
while ($i < $year+4) {
echo '<option>'.$i.'</option>';
$i++;
}
?>
</select>
<span style="margin-left:3px; margin-right:3px;"> to </span>
<select name="to_mo">
<option><?php echo $monthrangeend;?></option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
<select name="to_yr">
<option><?php echo $yearplustwo;?></option>
<?php
$year = date("Y");
$i = $year-1;
while ($i < $year+4) {
echo '<option>'.$i.'</option>';
$i++;
}
?>
</select>
- <?php
- if (isset($_GET['m'])) {
- if ($_GET['m'] == "Jan") {
- $monthrangeend = "Dec";
- }
- if ($_GET['m'] == "Feb") {
- $monthrangeend = "Jan";
- }
- if ($_GET['m'] == "Mar") {
- $monthrangeend = "Feb";
- }
- if ($_GET['m'] == "Apr") {
- $monthrangeend = "Mar";
- }
- if ($_GET['m'] == "May") {
- $monthrangeend = "Apr";
- }
- if ($_GET['m'] == "Jun") {
- $monthrangeend = "May";
- }
- if ($_GET['m'] == "Jul") {
- $monthrangeend = "Jun";
- }
- if ($_GET['m'] == "Aug") {
- $monthrangeend = "Jul";
- }
- if ($_GET['m'] == "Sep") {
- $monthrangeend = "Aug";
- }
- if ($_GET['m'] == "Oct") {
- $monthrangeend = "Sep";
- }
- if ($_GET['m'] == "Nov") {
- $monthrangeend = "Oct";
- }
- if ($_GET['m'] == "Dec") {
- $monthrangeend = "Nev";
- }
- } else {
- $monthrangeend = "";
- }
- if (isset($_GET['y'])) {
- $yearplustwo = $_GET['y'] + 2;
- } else {
- $yearplustwo = "";
- }
- ?>
- <form name="your form name" method="get" action="some action here">
- <span style="margin-right:3px;">Date Range (max range: 24 months)</span><br />
- <select name="from_mo" id="frommo">
- <option><?php echo $_GET["m"];?></option>
- <option>Jan</option>
- <option>Feb</option>
- <option>Mar</option>
- <option>Apr</option>
- <option>May</option>
- <option>Jun</option>
- <option>Jul</option>
- <option>Aug</option>
- <option>Sep</option>
- <option>Oct</option>
- <option>Nov</option>
- <option>Dec</option>
- </select>
- <select name="from_yr" id="fromyr" onchange="window.location.href='/members/index.php?m=' + this.form.frommo.options[this.form.frommo.selectedIndex].value + '&y=' + this.form.fromyr.options[this.form.fromyr.selectedIndex].value">
- <option><?php echo $_GET["y"];?></option>
- <?php
- $year = date("Y");
- $i = $year-1;
- while ($i < $year+4) {
- echo '<option>'.$i.'</option>';
- $i++;
- }
- ?>
- </select>
- <span style="margin-left:3px; margin-right:3px;"> to </span>
- <select name="to_mo">
- <option><?php echo $monthrangeend;?></option>
- <option>Jan</option>
- <option>Feb</option>
- <option>Mar</option>
- <option>Apr</option>
- <option>May</option>
- <option>Jun</option>
- <option>Jul</option>
- <option>Aug</option>
- <option>Sep</option>
- <option>Oct</option>
- <option>Nov</option>
- <option>Dec</option>
- </select>
- <select name="to_yr">
- <option><?php echo $yearplustwo;?></option>
- <?php
- $year = date("Y");
- $i = $year-1;
- while ($i < $year+4) {
- echo '<option>'.$i.'</option>';
- $i++;
- }
- ?>
- </select>
- Bigwebmaster
- Site Admin


- Joined: Dec 20, 2002
- Posts: 8922
- Loc: Seattle, WA & Phoenix, AZ
- Status: Offline
Fantastic! Glad you did find a solution, and thanks for posting how you did it. Looks like you ended up doing a combo of PHP and JavaScript just like I think I would have done 
Ozzu Hosting - Want your website on a fast server like Ozzu?
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 4 posts
- Users browsing this forum: No registered users and 224 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
