0

i can filter data and display output but i cannot maintain the state of filter checked on refresh or on going to nextpage.here is my code for filter.

$(document).ready(function(){

filter_data();

function filter_data()
{
    $('.filter_data').html('<div id="loading" style="" ></div>');
    var action = 'fetch_data';
    var minimum_price = $('#hidden_minimum_price').val();
    var maximum_price = $('#hidden_maximum_price').val();
    var maxprice = $('#hidden_maximum_price_mob').val();
    var minprice= $('#hidden_minimum_price_mob').val();
    var typeofproperty = get_filter('typeofproperty');
    var area = get_filter('area');
    var fortypepro = get_filter('fortypepro');
    $.ajax({
        url:"fetch_data.php",
        method:"POST",
        data:{action:action, minimum_price:minimum_price, maximum_price:maximum_price, typeofproperty:typeofproperty, area:area, fortypepro:fortypepro,maxprice:maxprice,minprice:minprice},
        success:function(data){
            $('.filter_data').html(data);
        }
    });
}

function get_filter(class_name)
{
    var filter = [];
    $('.'+class_name+':checked').each(function(){
        filter.push($(this).val());
    });
    return filter;
}

$('.common_selector').click(function(){
    filter_data();
});

and here is my logic to filter.

if(isset($_POST["area"]))
 {
  $area_filter = implode("','", $_POST["area"]);
  $query .= "
   AND area IN('".$area_filter."')
  ";
 }
 $query .= " 
 LIMIT  $this_page_first_result , $results_per_page
 ";
 $statement=mysqli_query($conn,$query);
 $total_row=mysqli_num_rows($statement);
 $output = '';
 if($total_row > 0)
 {//output

my pagination logic is this.

                 $results_per_page = 7;
                 $number_of_results=mysqli_num_rows($resul);
                 $number_of_pages = ceil($number_of_results/$results_per_page);
                 if (!isset($_GET['page'])) {
                              $page = 1;
                            } else {
                              $page = $_GET['page'];
                            }
                 $this_page_first_result = ($page-1)*$results_per_page;
                 $current_page = isset($get['page'])?$get['page'] : 1;
                 for($i=1;$i<=$number_of_pages;$i++){
                 $get['page'] = $i;
     $qs = http_build_query($get,'','&amp;');
     if($i==$current_page) {//display pagination links.

I want to know how to store checked filter data in a varaible,so that i can maitain filter data on refresh ,also logic on how to maintain pagination after filtering data(for example:if a user uses filter on page 5 he should be redirected to page 1 with filtered results,).I know its asking a lot but if not possible please point me in a direction where i get to know this kind of stuff.i mean ajax jquery php mysql filter.

Brent Worden
  • 10,624
  • 7
  • 52
  • 57
  • use GET instead of POST – Rao DYC Jan 29 '22 at 03:34
  • $number_of_results=mysqli_num_rows($resul); is it $resul or $result ? – Rao DYC Jan 29 '22 at 03:37
  • it it the number of products to be displayed fetched from the database..total no of products – ABHINAV PANDEY Jan 29 '22 at 11:45
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Jan 29 '22 at 15:21
  • come on guys ,atleast give me a link to this kind of project which has pagination and filter option. – ABHINAV PANDEY Jan 31 '22 at 14:30

0 Answers0