获取多个过滤器值参数

时间:2018-04-08 03:05:06

标签: php query-string

我正在使用此SO question来使用复选框处理我的过滤搜索。

这是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";

是否可以这样做?

3 个答案:

答案 0 :(得分:4)

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

  1. parse 查询字符串到数组中:

    extract($result);
    
  2. 然后,extract将数组作为变量:

    extract
  3. 有几点需要注意:

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

    我会跳过第2步($result),并直接使用echo $result['discount'],例如var connectionOptions = { "force new connection" : true, "reconnectionAttempts": "Infinity", //avoid having user reconnect manually in order to prevent dead clients after a server restart "timeout" : 10000, //before connect_error and connect_timeout are emitted. "transports" : ["websocket"], "secure" : true }; SOCKET = io('wss://my_site:8443', connectionOptions);

答案 1 :(得分:0)

这听起来像是在混合帖子和获取,是不是你要追求的?

通过GET:

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 = '';
}

答案 2 :(得分:0)

在某种程度上,您可以在php中使用explode功能将您的项目与fdata

分开

你可以在你的客户端JS app中定义一些字符,例如(,)然后在php中的explode函数中你必须设置分隔符等于逗号字符

在PHP中爆炸功能

xrandr
示例分隔符中的

是逗号,字符串是fdata(限制可选 )

explode(separator,string,limit)

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

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

然后像这样的$ arr_变量

para1=223&para2=4353&para3=234

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