<select> Month & Year help

What I am trying to do is select month & year and store them to variables.
The code should automatically select the month & year by default. ( It does ), but does not store the data to variables.
When I select month , it shows month correctly but on selecting year ( it show year correctly) the month changes to current month. Only the year variable is stored correctly, the month variable changes to the current month!

<?php

$month_range=array("1" =>"January","2"=>"Feburary", "3"=>"March","4"=>"April","5"=>"May","6"=>"June","7"=>"July","8"=>"August","9"=>"September","10"=>"October","11"=>"November","12"=>"December");
$year_range=range(2019, 2050);
$current_month=date('F');
$current_year=date('Y')
?>
<!DOCTYPE html>
<html lang="en">
<form>
<select name="month" onchange="this.form.submit()">
<option value=''>Select Month</option>
<?php
foreach($month_range as $month) {
$selected_month = ($month == $current_month) ? 'selected' : '';
echo '<option '.$selected_month.' value ="' .$month.'">'.$month.'</option>'; }
?>
</select>
<select name="year" id="year" onchange="this.form.submit()")>
<option value=''> Select Year</option>
<?php
foreach($year_range as $year) {
$selected_year = ($year == $current_year) ? 'selected' : '';
echo '<option '.$selected_year.' value ="' .$year.'">'.$year.'</option>';
}
?>
</select>
<?php
if(isset($_GET['year'])) {
$month=$_GET['month'];
$year=$_GET['year'];
echo $month;
echo $year;
}
?>
</form>
</html>

Your program logic should use the current month/year, as default values for the inputs, if there is no existing $_GET['month']/$_GET['year'] input. Your form would then use whatever the result is of that logic to determine which option is selected -

<?php

$month_range=array("1" =>"January","2"=>"February", "3"=>"March","4"=>"April","5"=>"May","6"=>"June","7"=>"July","8"=>"August","9"=>"September","10"=>"October","11"=>"November","12"=>"December");
$year_range=range(2019, 2050);


// condition, validate, and set default values for inputs - month, year
// note: you should submit the month number, not the month name

$errors = []; // an array to hold validation/user error messages

$month = isset($_GET['month']) ? (int)$_GET['month'] : date('n');
if(!isset($month_range[$month]))
{
	$errors['month'] = 'Invalid Month selected.'; // this error would either be due to a programming mistake or someone submitting their own values to your code
}

$year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');
if(!in_array($year, $year_range))
{
	$errors['year'] = 'Invalid Year selected.'; // this error would either be due to a programming mistake or someone submitting their own values to your code
}

?>
<!DOCTYPE html>
<html lang="en">
<?php
// display any errors
if(!empty($errors))
{
	echo '<p>'; echo implode('<br>',$errors); echo '</p>';
}
?>
<form>
<select name="month" onchange="this.form.submit()">
<option value=''>Select Month</option>
<?php
foreach($month_range as $key=>$value)
{
	$sel = $month == $key ? 'selected' : '';
	echo "<option $sel value ='$key'>$value</option>";
}
?>
</select>
<select name="year" onchange="this.form.submit()")>
<option value=''> Select Year</option>
<?php
foreach($year_range as $value)
{
	$sel = $year == $value ? 'selected' : '';
	echo "<option $sel value ='$value'>$value</option>";
}
?>
</select>
</form>
<?php
echo $month;
echo '<br>';
echo $year;
?>
</html>

Thanks a lot, learning a lot!

Sponsor our Newsletter | Privacy Policy | Terms of Service