3

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> 
BernardA
  • 1,391
  • 19
  • 48
  • I assume this is PHP, yes? You might want to add a [tag:PHP] tag to your question. – GordonM Apr 02 '13 at 17:26
  • What does the JavaScript code look like that sends the request? Looks as though it is sending an encoded `hook[2]=107`. – jeffjenx Apr 02 '13 at 17:33
  • Sorry for the mess. I added the js script to the original post, – BernardA Apr 02 '13 at 17:37
  • Answering MrSlayer. Yes that is correct. Those are form checkboxes named hook[number]. Meaning that in the case the 3rd checkbox with the value of 107 was checked. – BernardA Apr 02 '13 at 17:49
  • @BernardA in that case, there's an issue with the JavaScript serialisation rather than the PHP deserialisation. Perhaps you could provide more details about the JS plugin (is it [DataTables](http://datatables.net/))? – cmbuckley Apr 02 '13 at 18:08
  • Thanks cbuckley. Yes it is DataTables. The serialization as such is done outside DataTables as you can see in my script and I do not think there is a problem with it. I have the same serialization done in another page with a regular Ajax call with method POST and it works fine. It is received properly at destination. – BernardA Apr 02 '13 at 20:24
  • As no-one seems to have mentioned it yet - how much data are you actually trying to send via `GET` because it's limited. According to [this answer](http://stackoverflow.com/a/7725515/1238344) it varies but the limit is always significantly smaller than the amount of data you are allowed to send via `POST`. – Emissary Apr 02 '13 at 20:54
  • Hi Emissary, as you can see in the $_GET dump on my post, it is a very short string "hook%5B2%5D=107". It can get 3-4 times as long, but that would still not be much.Thanks. – BernardA Apr 02 '13 at 22:15
  • @BernardA Okay dokes - you did say, *"this as an example"* so I was just covering all bases :) – Emissary Apr 03 '13 at 07:01

3 Answers3

1

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.

scones
  • 3,317
  • 23
  • 34
  • Thanks Scones. Let's see if I can answer your questions. Concerning the custom sending method, I am fully in agreement with you, but that's how the plugin is set up. So fnReloadAjax is in a way a 'customized' Ajax call. I had the same set up in Ajax POST method and everything worked fine. But if I want to have the plugin for tables, which has definetely a number of pluses, I need to adapt. Concerning the content of str in the case is "hook%5B2%5D=107". That being the result of jquery serialization() as explained in my comment back to MrSlayer. – BernardA Apr 02 '13 at 20:18
  • try `$('input[name=hook]:checked', '#filter').val();` instead of `str = $("#filter").serialize();` and it should work. so you would be just passing along the value – scones Apr 03 '13 at 05:30
  • Thanks Scones. Following your comments I reversed course and went back to the origin of the information, rather than at the receiving end. So I tried this 'code' $('#filter :checked').each(function() {str.push($(this).val());});'code' instead of serialize() and it worked fine. I am giving you credit for pointing me in the right direction. – BernardA Apr 03 '13 at 11:01
0
/**
 * 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];
}
Petrovitch
  • 139
  • 3
  • 11
  • Hi Kenn. It did the job when there is only one value coming from the script, but it did not work when more than one are coming. How do I make it work for multiple values. I tried to make $val an array, but it did not agree with me. Thanks. – BernardA Apr 02 '13 at 18:04
  • It's a little more complicated, but you could use preg_match_all, then foreach ($matches[0] as $match). Too, I think you are serializing the token (key op val) rather than just the val. Try .value.serialize or .text.serialize – Petrovitch Apr 02 '13 at 20:29
0

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? This behaviour is not part of JQuery I guess (see: http://api.jquery.com/serialize/).

user1995621
  • 47
  • 3
  • 9
  • Thanks user...This is actually exactly what the code str = $("#filter").serialize();, does when one checks this: . I have exactly the same script in another php file and the result is precisely the same. Only difference is that in that case it is a regular Ajax call with POST method which works without a hitch. – BernardA Apr 02 '13 at 23:21