I am having a problem here. I have a dropdown whereby upon selecting an option I get certain data from the database using jQuery. Everything works fine except that the Ajax works only once. After selecting the first option the data is derived from the database and everything works well but when I try selecting another option the Ajax doesn't call and I don't see anything in the console.i am using a class to call the jQuery function.where might I going wrong.
Here is my code - in the blade file:
<form name="sortproducts" id="sortproducts">
<input type="text" name="url" id="url" value="{{ $url }}" style="display:none">
<select name="sort" class="sortprods" id="sort">
<option value="">Default Sorting</option>
<option value="latest_products"
@if (isset($_GET['sort']) && $_GET['sort']=="latest_products")
selected=""
@endif
>Latest Products</option>
<option value="most_popular"
@if (isset($_GET['sort']) && $_GET['sort']=="most_popular")
selected=""
@endif
>Most Popular</option>
<option value="low_to_high"
@if (isset($_GET['sort']) && $_GET['sort']=="low_to_high")
selected=""
@endif
>Price:Low to High</option>
<option value="high_to_low"
@if (isset($_GET['sort']) && $_GET['sort']=="high_to_low")
selected=""
@endif
>Price:High to Low</option>
<option value="product_name_a_z"
@if (isset($_GET['sort']) && $_GET['sort']=="product_name_a_z")
selected=""
@endif
>Product Name:A to Z</option>
<option value="product_name_z_a"
@if (isset($_GET['sort']) && $_GET['sort']=="product_name_z_a")
selected=""
@endif
>Product Name:Z to A</option>
</select>
</form>
The jQuery code:
$(document).ready(function() {
// sort products on dropdown
$(".sortprods").on('change',function() {
var sortproducts=$(this).val();
alert(sortproducts);
console.log(sortproducts);
$.ajax({
url:'/'.$url,
method:"get",
data:{
sort:sortproducts
},
success:function(data){
$('.showproducts').html(data);
}
});
});
});