I'm having problems with updating the products page on dropdown change using PHP in combination with Ajax. Right now it's not updating the query when I select an item from the dropdown menu. The query itself is working fine.
What I want is: when a user selects a different option from any of multiple dropdown menus, the query is updated by means of an Ajax POST request. Thus the results get filtered by the selection of the dropdown.
The HTML:
<select id="Gender">
<option value="" disabled selected>--Geslacht--</option>
<option value="1">Man</option>
<option value="2">Vrouw</option>
<option value="3">Beide</option>
</select>
The PHP MySQL query to select the data:
$statements = '';
if (isset($_POST['GenderID'])) {
$gender_query = $_POST['GenderID'];
$statements .= " AND `product`.`gender_id` = '$gender_query' "; //condition for each property
}
$query = "SELECT `product`.`product_name` AS pname,
`product`.`product_img` AS pimg,
`brand`.`brand_name` AS bname
FROM
`product`
INNER JOIN
`brand`
ON
(`product`.`brand_id`=`brand`.`brand_id`)
WHERE
`product`.`active` = '1' $statements";
$result2 = $conn->query($query);
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc())
{
echo'
<div class="col-lg-4">
<div class="card">
<div class="view overlay">
<a href="#">
<img src="products/',$row["pimg"],'" class="img-fluid">
</a>
</div>
<div class="card-body">
<h4 class="card-title">',$row["bname"],'</h4>
<p class="card-text">',$row["pname"],'</p>
<a href="#" class="btn btn-outline-success my-2 my-sm-0">Button</a>
</div>
</div>
</div>';
}
}
The JS Jquery/Ajax:
$(document).ready(function(){
$("#Gender").change(function () {
var gender = $("#Gender").val();
jQuery.ajax({
type: "POST",
data: {GenderID: gender},
success: function(data){
if(data.success == true){
alert('success');
}
}
});
});
});
It's probably the smallest mistake that I'm making. Any help is much appreciated.
UPDATE
I've added the code of what executes the Mysql query.
$query
after you create it?