0

I have this below function to filter search

function GetFilters()
{
    $('input[type="checkbox"]').on('change', function (e) {
        var data = {},
          fdata = [],
          loc = $('<a>', { href: window.location })[0];

          var categoryid = loc.pathname.split('/').pop();

      $('input[type="checkbox"]').each(function (i) {
          if (this.checked) {
              if (!data.hasOwnProperty(this.name)) {
                  data[this.name] = [];
              }
              data[this.name].push(this.value);
          }
      });

      // get all keys.
      var keys = Object.keys(data);
      var fdata = "";
      // iterate over them and create the fdata
      keys.forEach(function(key,i){
          if (i>0) fdata += '&'; // if its not the first key add &
          fdata += key+"="+data[key].join(',');
      });

      $.ajax({
        type: "get",
        url: "<?php echo $appURL; ?>/productByCategory",
        data: {
              "fdata": fdata,
              "categoryID": categoryid
            },
        success: function (response) {
          $('.productCategory').html(response);
        }
      });
      if (history.pushState) {
          history.pushState(null, null, loc.pathname + '?' + fdata);
      }
  });
}

window.onload = GetFilters;

and now I want to run the function every page loaded, but it seems there is no effect and display is empty.

PHP

if(isset( $_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'))
{
}

If I remove this: if(isset( $_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) { } I can see the data display.

I need to do this because prevent user directly open productByCategory.php

How to do that?

HiDayurie Dave
  • 1,586
  • 1
  • 14
  • 31
  • Have you take a look at this article? https://paulund.co.uk/use-php-to-detect-an-ajax-request – Muhammad Ibnuh Apr 08 '18 at 12:27
  • 1
    Possible duplicate of [How to check if the request is an AJAX request with PHP](https://stackoverflow.com/questions/18260537/how-to-check-if-the-request-is-an-ajax-request-with-php) – Muhammad Ibnuh Apr 08 '18 at 12:28

0 Answers0