Issue
Hie, i am trying to filter based on url change on two select options. Not sure how to go about it but i tried something below and nothing seems to be working Html code
<form class="form-inline">
<label>Select Year: </label>
<select class="form-control input-sm" id="select_year">
<?php
date_default_timezone_set('Africa/Johannesburg');
$today = date('y-m-d');
$year = date('Y');
$currentyear = date('Y');
if(isset($_GET['year'])){
$year = $_GET['year'];
}
for($i=2019; $i<=$currentyear; $i++){
$selected = ($i==$year)?'selected':'';
echo "
<option value='".$i."' ".$selected.">".$i."</option>
";
}
?>
</select>
<label>Select Continent: </label>
<select class="form-control input-sm" id="select_continent">
<option value="Africa">Africa</option>
<option value="Americas/Central/South/Latin/">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="Europe">Europe</option>
</select>
<button type="button" id="filter-form">Filter</button>
</form>
Js Code
$(function(){
$('#filter-form').click(function(){
window.location.href = 'view-report.php?year='+$('select_year').val()'&continent='+$('#select_continent').val();
});
Solution
You're making this more complex than it needs to be - you can just use a FORM submitting via GET and it will produce the URL you want.
<form method="get" action="view-report.php" class="form-inline">
<label for="select_year">Select Year: </label>
<select name="year" class="form-control input-sm" id="select_year">
<?php
date_default_timezone_set('Africa/Johannesburg');
$year_option = 2019;
$year_current = $year_selected = date('Y');
if( isset( $_GET['year'] ) )
$year_selected = $_GET['year'];
while( $year_option <= $year_current ){
echo sprintf( '<option%s>%s</option>' ,
( $year_option == $year_selected ? ' selected="selected"' : '' ) ,
$year_option
);
$year_option++;
}
?>
</select>
<label for="select_continent">Select Continent: </label>
<select name="continent" class="form-control input-sm" id="select_continent">
<?php
$continent_list = array(
'Africa' => 'Africa' ,
'America' => 'Americas/Central/South/Latin' ,
'Australia' => 'Australia' ,
'Europe' => 'Europe'
);
$continent_selected = false;
if( isset( $_GET['continent'] ) )
$continent_selected = $_GET['continent'];
foreach( $continent_list as $label => $value ){
echo sprintf( '<option value="%s"%s>%s</option>' ,
$value ,
( $value == $continent_selected ? ' selected="selected"' : '' ) ,
$label
);
}
?>
</select>
<button type="submit">Filter</button>
</form>
Answered By - Luke Stevenson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.