简体   繁体   English

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

[英]$_GET not deserializing/getting the value from checked check-boxes

I do not have much experience with $_GET . 我对$_GET没有多少经验。 I try to always use POST method, so I may be missing something obvious. 我尝试总是使用POST方法,所以我可能会遗漏一些明显的东西。

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. 我正在使用一个插件,它通过GET方法向服务器发送请求,其中一个序列化形式的值使用表单中的jQuery serialize()完成。

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. 通常,至少在使用$_POST ,该值可以很容易地分配给接收PHP文件中的工作变量。 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. 当我尝试用$array = unserialize($string)序列化它时,它给出了一个bool(false)返回。

A var_dump($_GET) on the receiving end gives this as an example: 接收端的var_dump($_GET)以此为例:

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. 在类似情况下的var_dump($_POST)会直接显示值'107',这正是我要找的。

I appreciate your help. 我感谢您的帮助。

Here is the Javascript: 这是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. 为了将来的参考,我将在看到来自Scones的评论之后修改脚本。 So the solution was to get back to the originating php file and change the way the values were retrieved. 所以解决方案是回到原始的php文件并改变检索值的方式。 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. 从表单中获取值的方法是从serialize()更改为了容纳插件,这只能处理硬数字。 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> 

The answer deals with wrong assumptions. 答案涉及错误的假设。 The implied assumption: "The javascript was sent correctly to the php" is wrong. 隐含的假设:“javascript被正确发送到php”是错误的。

PHP receives the string "?hook=hook%2b2%2d=107&_=1364920519074" and turns it into the array displayed above. PHP接收字符串“?hook = hook%2b2%2d = 107&_ = 1364920519074”并将其转换为上面显示的数组。

There are 2 questions comming to mind: 有两个问题需要考虑:

  • why use a custom sending method instead of predefined ones like $.ajax() ? 为什么使用自定义发送方法而不是预定义的发送方法,如$.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 });}, aoData.push({name : 'hook', value : str });},提供的javascript中str的真实内容是什么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. 如果没有关于functin dataTable和生成的html的更多内容,就没有更多关于这个问题的说法。

/**
 * Temp fix to get value. Try serializing the contents of the variable, and not the 
 * variable itself.
 */
if (preg_match('/\=(.*)/', $_GET["hook"], $match))
{
  $val = $match[1];
}

I would recommend you to write your own serialization method to solve the problem forever. 我建议你编写自己的序列化方法来永远解决问题。 But I have a question, have you found out from where and why this serialization id is generated for the checkboxes? 但我有一个问题,你有没有找到从哪里以及为什么为复选框生成这个序列化ID? This behaviour is not part of JQuery I guess (see: http://api.jquery.com/serialize/ ). 我猜这种行为不是JQuery的一部分(参见: http//api.jquery.com/serialize/ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM