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条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式