【问题标题】:Get multiple filter value parameters获取多个过滤器值参数
【发布时间】:2018-04-08 03:05:05
【问题描述】:

我正在使用这个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";

这样可以吗?

【问题讨论】:

  • fdata保存为对象
  • 嗨@Hikarunomemory 你能给我举个例子吗?
  • 你试过$_GET['fdata']['discount'];吗?
  • 刚刚尝试并得到错误:Warning: Illegal string offset 'discount' in
  • 这里有一个类似的帖子link

标签: php query-string


【解决方案1】:

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

  1. parse查询字符串转换成数组:

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

    extract($result);
    

需要注意的几点:

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

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

【讨论】:

    【解决方案2】:

    听起来你正在混合 post 和 get,你追求的是这样的东西吗?

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

    【讨论】:

      【解决方案3】:

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

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

      PHP中的explode函数

      explode(separator,string,limit)
      

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

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

      如果你在 fdata 字符串中有类似的东西

      para1=223&para2=4353&para3=234
      

      然后像这样的 $arr_ 变量

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

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

      【讨论】:

        猜你喜欢
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        • 2021-03-22
        • 2016-12-30
        • 1970-01-01
        • 2019-07-22
        • 1970-01-01
        • 2016-09-23
        相关资源
        最近更新 更多