Renzo Diaz Renzo Diaz - 4 months ago 10
MySQL Question

How submit multiple dropdown on change

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!

Answer

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();
    });
}