So i have created Four dropdown list on index.php
<select name="filter_month" class="filters">
<option>Select a Month</option>
<option value="1">January</option>
<option value="2">February</option>
....
</select>
<select name="filter_year" class="fitlers">
<option>Select a Year</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
<select name="filter_category" class="filters">
<option>Select a Category</option>
<option value="1">Running</option>
<option value="2">Trail Running</option>
</select>
<select name="filter_country" class="filters">
<option>Select a Country</option>
<option value="1">Canada</option>
<option value="2">United States</option>
</select>
Now what i wont to do is create a filter with each selected values from dropdown and send post to filters.php
$(function) {
$('.filters').on('change', function(){
// How send value from each Dropdown?
});
}
on filters.php i have created this code, how return data if i have selected any of these options ($mm, $yy, $cat ,$country)
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$mm = $_POST['filter_month'];
$yy = $_POST['filter_year'];
$cat = $_POST['filter_category'];
$country = $_POST['filter_country'];
// ... connection to database
$query = "how can filter by month, year, category and country";
// ... Execute query
// ... while ...
echo $query;
// ... end while ...
}
Thank you!
You should put the selects in a HTML form (for example with id myForm
and send it with jQuery.
$(function) {
$('.filters').on('change', function(){
$('#myForm').submit();
});
}
First Give an Id to each options like below
<select name="filter_month" id="filter_month" class="filters">
<option>Select a Month</option>
<option value="1">January</option>
<option value="2">February</option>
....
</select>
<select name="filter_year" id="filter_year" class="fitlers">
<option>Select a Year</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
<select name="filter_category" id="filter_category" class="filters">
<option>Select a Category</option>
<option value="1">Running</option>
<option value="2">Trail Running</option>
</select>
<select name="filter_country" id="filter_country" class="filters">
<option>Select a Country</option>
<option value="1">Canada</option>
<option value="2">United States</option>
</select>
In jQuery code
<script>
$(function) {
$('.filters').on('change', function () {
$.ajax({
type: 'POST',
url: 'filters.php',
data: {
filter_month: $("#filter_month").val(),
filter_year: $("#filter_year").val(),
filter_category: $("#filter_category").val(),
filter_country: $("#filter_country").val()
},
success: function (data) {
console.log(data);
}});
});
}
</script>
server side code
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$mm = $_POST['filter_month'];
$yy = $_POST['filter_year'];
$cat = $_POST['filter_category'];
$country = $_POST['filter_country'];
// ... connection to database
$query = "how can filter by month, year, category and country";
// ... Execute query
// ... while ...
?>
put html code here and set the values
<?php
// ... end while ...
}