Have not found anyhing on stack
My sorting method is:
if($_GET['f1']=='ASC')
{
$order = ' ORDER BY clients.name ASC ';
$f1 = '<button type="button" class="btn btn-link btn-sm f1" id="DESC"><i class="bi bi-sort-alpha-down-alt"></i></button>';
}
elseif($_GET['f1']=='DESC')
{
$order = ' ORDER BY clients.name DESC ';
$f1 = '<button type="button" class="btn btn-link btn-sm f1" id="ASC"><i class="bi bi-sort-alpha-down"></i></button>';
}
else
{
$f1 = '<button type="button" class="btn btn-link btn-sm f1" id="ASC"><i class="bi bi-sort-alpha-down"></i></button>';
}
The same logic I have for f2, f3 and f4
My AJAX for f1 is like:
$(document).on("click",".f1",function(){
let id = $(this).attr('id');
$.ajax({
method:'GET',
url:'ajax.php?x=clients',
data:{f1:id},
success:function(response){
$('#data').html(response);
}
})
})
Everythign works properly but I would like to make sorting for all methods (f1,f2,f3,f4) using one ajax function
Please advise the best way to make it without total modification of any of the methods
2
Answers
You can do this with a few small changes I think:
id="ASC"
like that because IDs must be unique in a HTML document, and clearly you could end up with more than one like that, across the 4 methods).For example:
and
Then in the JS:
.
Obviously you should then amend your PHP to look for
$_GET['field'] and
$_GET[‘direction’]`, to match the AJAX parameter names (I felt these were a lot easier to understand than "x" and "f1", which are pretty meaningless, but you may disagree and change them back if you wish).You can use wildcard on
class
before send data.