dongzaocheng3214 2013-04-02 17:25
浏览 146
已采纳

$ _GET不反序列化/获取已选中复选框的值

I do not have much experience with $_GET. I try to always use POST method, so I may be missing something obvious.

I am utilizing a plugin, which sends a request to server via GET method, with a value in a serialized form done with jQuery serialize() out of a form.

Normally, at least when working with $_POST, the value would be readily available to be assigned to a working variable at the receiving PHP file. This is not happening as far as I can tell.

It keep its serialized representation. When I tried to unserialize it with $array = unserialize($string), it gave a bool(false) return.

A var_dump($_GET) on the receiving end gives this as an example:

array(2) {
  ["hook"]=>
  string(15) "hook%5B2%5D=107"
  ["_"]=>
  string(13) "1364920519074" //This is a serial no. generated by the plugin
}

A var_dump($_POST) in a similar situation would show directly the value '107', which is what I am looking for.

I appreciate your help.

Here is the Javascript:

$(document).ready(function () {
  var str;
  oTable = $("#myTable").dataTable({
    "fnServerParams": function (aoData) {
      aoData.push({
        name: 'hook',
        value: str
      });
    },
  });
  $("#filter").change(function () {
    str = $("#filter").serialize();
    oTable.fnReloadAjax("filter_prange.php");
  });
});

For future reference I am showing the revised script after comments from Scones. So the solution was to get back to the originating php file and change the way the values were retrieved. The method of getting the values from the form was changed from serialize() in order to accomodate the plug in, which can only deal with hard numbers. So this actually show 2 different way of retrieving the values out of checked check-boxes. I amended the title accordingly.

<script>
$(document).ready(function() {
    var str = [];
        oTable = $("#myTable").dataTable({
    "fnServerParams": function (aoData) {
        aoData.push({ name:'str', value:str });}
});

$("#filter").change(function(){
str=[];
 $('#filter :checked').each(function() {
   str.push($(this).val());
 });
oTable.fnReloadAjax("filter_prange.php");
 });

});

And this is the form that triggers all this:

                           <form id = "filter">
<input class="hook1" type="checkbox" value="115" name="hook[0]">  
<input class="hook1" type="checkbox" value="116" name="hook[1]"> 
<input class="hook1" type="checkbox" value="107" name="hook[2]">
</form> 
  • 写回答

3条回答 默认 最新

  • douyouchou1085 2013-04-02 18:17
    关注

    The answer deals with wrong assumptions. The implied assumption: "The javascript was sent correctly to the php" is wrong.

    PHP receives the string "?hook=hook%2b2%2d=107&_=1364920519074" and turns it into the array displayed above.

    There are 2 questions comming to mind:

    • why use a custom sending method instead of predefined ones like $.ajax()? Do you use another framework for data transfer?
    • what is the real content of str in the supplied javascript in aoData.push({name : 'hook', value : str });},

    Without further content about the functin dataTable and the generated html, there can be nothing more said about the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向