获取多个过滤值参数

Get multiple filter value parameters

我正在使用这个 来使用复选框处理我的过滤器搜索。

这是JS

$('input[type="checkbox"]').on('change', function (e) {
      var data = {},
          fdata = [],
          loc = $('<a>', { href: window.location })[0];
      $('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: "/ajax/get",
        data: {
              "_token": "{{ csrf_token() }}",
              "fdata": fdata
            },
        success: function (response) {
          $('#d2d-results').html(response);
        }
      });
      if (history.pushState) {
          history.pushState(null, null, loc.pathname + '?' + fdata);
      }
  });

现在我尝试将 fdata 的值设为 PHP。

在 PHP 我得到变量的这个值 echo $_GET['fdata'];:

discount=Y&brand=BR0006,BR0003

我想要的

$discount="Y";
$brand="BR0006,BR0003";

这样可以吗?

听起来你在混音post然后得到,你想要的是这样的吗?

通过获取:

if(isset($_GET['discount'])) {
    $discount = $_GET['discount'];
} else {
    $discount = '';
}

if(isset($_GET['brand'])) {
    $brand = $_GET['brand'];
} else {
    $brand = '';
}

POST方法:

if(isset($_POST['discount'])) {
    $discount = $_POST['discount'];
} else {
    $discount = '';
}

if(isset($_POST['brand'])) {
    $brand = $_POST['brand'];
} else {
    $brand = '';
}

在一种方式中,您可以使用 php 中的 explode 函数将您的项目与 fdata 分开

您可以在客户端 JS 应用程序中定义一些字符,例如 (,),然后在 php 的爆炸函数中,您必须设置分隔符等于逗号字符

PHP

中的分解函数
explode(separator,string,limit)

在您的示例中,分隔符是逗号,字符串是 fdata( limit 可选 )

$fdata = $_GET['fdata'];
$arr_ = explode('&',$fdata);

如果你在 fdata 字符串中有这样的东西

para1=223&para2=4353&para3=234

然后$arr_这样的变量

$arr_ = [para1=223 , para2=4353 , para3=234];

如果你想要单独的 value 和 key ,你可以再次这样做并使用循环

做你想做的,你必须做两个步骤:

  1. parse查询字符串转化为数组:

    parse_str($_GET['fdata'], $result);
    
  2. 然后,extract数组作为变量:

    extract($result);
    

注意几点:

使用 extract 非常不安全(而且有点难看)。用户可以在 URL 中放置(例如)isAdmin=1 之类的内容,这将影响您的代码。基本上,你不能再相信你的变量了。

我会跳过第 2 步(extract 东西),直接使用 $result,例如 echo $result['discount'].