javsmo
javsmo

Reputation: 1337

jQuery $.get data doesn't appear in PHP $_GET

I'm using jQuery $.get to send some data to my PHP server as the example below:

Javascript:

var usr_data = JSON.stringify(window.fb_user);
var save_data = {
    cmd         : 'new_suggestion',
    suggestion  : $('#suggestion').val(),
    sender      : usr_data
};

$.get('/class/suggestion.php',
save_data,
function( data ) {
    if (data.result){
        alert('Thanks for your suggestion.');
    } else {
        alert('Error');
    }           
});

PHP:

var_dump($_GET);

The usr_data variable is an object containing the Facebook user data that I get when user logs in with his Facebook account.

The problem is that sometimes, depending on available user data, the sender variable disappear as below:

array(2) {
  ["cmd"]=>
  string(14) "new_suggestion"
  ["suggestion"]=>
  string(15) "Some suggestion"
}

I can't post real sender data here, because it's Facebook user data and I couldn't find a way to reproduce the error with fake data or with my facebook data.

EDIT

Does anybody know why sender isn't appearing on $_GET array?

EDIT II

I just found an URL with fake data that reproduce the error.

I created a PHP file called test_get.php with just the line below.

<?php var_dump($_GET); ?>

And used the URL below:

http://mysite/test_get.php?cmd=new_suggestion&suggestion=some%20suggestion&sender=%7B%22id%22%3A%221234567890123%22%2C%22name%22%3A%22Foo%20Bar%20Baz%22%2C%22first_name%22%3A%22Foo%22%2C%22middle_name%22%3A%22Bar%22%2C%22last_name%22%3A%22Baz%22%2C%22link%22%3A%22https%3A%2F%2Fwww.facebook.com%2Ffoobarbaztest%22%2C%22username%22%3A%22foobarbaz%22%2C%22birthday%22%3A%2201%2F01%2F1900%22%2C%22hometown%22%3A%7B%22id%22%3A%221234567890%22%2C%22name%22%3A%22Test%20City%22%7D%2C%22inspirational_people%22%3A%5B%7B%22id%22%3A%22111713915507379%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%2C%7B%22id%22%3A%22106184866078691%22%2C%22name%22%3A%22Romário%22%7D%2C%7B%22id%22%3A%22109601749058079%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%5D%2C%22gender%22%3A%22male%22%2C%22religion%22%3A%22Cristão%20-%20Católico%22%2C%22political%22%3A%22Foo%20Party%22%2C%22email%22%3A%22foobarbaz%40foobar.foo%22%2C%22timezone%22%3A-5%2C%22locale%22%3A%22pt_BR%22%2C%22languages%22%3A%5B%7B%22id%22%3A%22108083115891989%22%2C%22name%22%3A%22Português%22%7D%2C%7B%22id%22%3A%22108177092548456%22%2C%22name%22%3A%22Español%22%7D%2C%7B%22id%22%3A%22106059522759137%22%2C%22name%22%3A%22English%22%7D%2C%7B%22id%22%3A%22108224912538348%22%2C%22name%22%3A%22French%22%7D%2C%7B%22id%22%3A%22108106272550772%22%2C%22name%22%3A%22French%22%7D%2C%7B%22id%22%3A%22450169151702580%22%2C%22name%22%3A%22Portuguese%22%7D%5D%2C%22verified%22%3Atrue%2C%22updated_time%22%3A%222011-11-02T18%3A43%3A36%2B0000%22%7D

The result is the same:

array(2) {
  ["cmd"]=>
  string(14) "new_suggestion"
  ["suggestion"]=>
  string(15) "some suggestion"
}

EDIT III

As suggested on comments, there were accented characters on URL. I encoded these characters and tried again with no luck. Here's the correctly encoded URL:

http://mysite/test_get.php?cmd=new_suggestion&suggestion=some%20suggestion&sender=%7B%22id%22%3A%221234567890123%22%2C%22name%22%3A%22Foo%20Bar%20Baz%22%2C%22first_name%22%3A%22Foo%22%2C%22middle_name%22%3A%22Bar%22%2C%22last_name%22%3A%22Baz%22%2C%22link%22%3A%22https%3A%2F%2Fwww.facebook.com%2Ffoobarbaztest%22%2C%22username%22%3A%22foobarbaz%22%2C%22birthday%22%3A%2201%2F01%2F1900%22%2C%22hometown%22%3A%7B%22id%22%3A%221234567890%22%2C%22name%22%3A%22Test%20City%22%7D%2C%22inspirational_people%22%3A%5B%7B%22id%22%3A%22111713915507379%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%2C%7B%22id%22%3A%22106184866078691%22%2C%22name%22%3A%22Rom%C3%A1rio%22%7D%2C%7B%22id%22%3A%22109601749058079%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%5D%2C%22gender%22%3A%22male%22%2C%22religion%22%3A%22Crist%C3%A3o%20-%20Cat%C3%B3lico%22%2C%22political%22%3A%22Foo%20Party%22%2C%22email%22%3A%22foobarbaz%40foobar.foo%22%2C%22timezone%22%3A-5%2C%22locale%22%3A%22pt_BR%22%2C%22languages%22%3A%5B%7B%22id%22%3A%22108083115891989%22%2C%22name%22%3A%22Portugu%C3%AAs%22%7D%2C%7B%22id%22%3A%22108177092548456%22%2C%22name%22%3A%22Espa%C3%B1ol%22%7D%2C%7B%22id%22%3A%22106059522759137%22%2C%22name%22%3A%22English%22%7D%2C%7B%22id%22%3A%22108224912538348%22%2C%22name%22%3A%22French%22%7D%2C%7B%22id%22%3A%22108106272550772%22%2C%22name%22%3A%22French%22%7D%2C%7B%22id%22%3A%22450169151702580%22%2C%22name%22%3A%22Portuguese%22%7D%5D%2C%22verified%22%3Atrue%2C%22updated_time%22%3A%222011-11-02T18%3A43%3A36%2B0000%22%7D

With this comment, I noticed that jQuery (or my Browser) isn't encoding the accented chars returned by JSON.stringify(window.fb_user);

EDIT IV

The following URL works, but if I add the final part of the e-mail "@foobar.foo" it fails. Also tried without the e-mail, but with other data, but it also fail.

cmd=new_suggestion&suggestion=some%20suggestion&sender=%7B%22id%22%3A%221234567890123%22%2C%22name%22%3A%22Foo%20Bar%20Baz%22%2C%22first_name%22%3A%22Foo%22%2C%22middle_name%22%3A%22Bar%22%2C%22last_name%22%3A%22Baz%22%2C%22link%22%3A%22https%3A%2F%2Fwww.facebook.com%2Ffoobarbaztest%22%2C%22username%22%3A%22foobarbaz%22%2C%22birthday%22%3A%2201%2F01%2F1900%22%2C%22hometown%22%3A%7B%22id%22%3A%221234567890%22%2C%22name%22%3A%22Test%20City%22%7D%2C%22inspirational_people%22%3A%5B%7B%22id%22%3A%22111713915507379%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%2C%7B%22id%22%3A%22106184866078691%22%2C%22name%22%3A%22Romário%22%7D%2C%7B%22id%22%3A%22109601749058079%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%5D%2C%22gender%22%3A%22male%22%2C%22religion%22%3A%22Cristão%20-%20Católico%22%2C%22political%22%3A%22Foo%20Party%22%2C%22email%22%3A%22foobarbaz

array(3) {
  ["cmd"]=>
  string(14) "new_suggestion"
  ["suggestion"]=>
  string(15) "some suggestion"
  ["sender"]=>
  string(509) "{"id":"1234567890123","name":"Foo Bar Baz","first_name":"Foo","middle_name":"Bar","last_name":"Baz","link":"https://www.facebook.com/foobarbaztest","username":"foobarbaz","birthday":"01/01/1900","hometown":{"id":"1234567890","name":"Test City"},"inspirational_people":[{"id":"111713915507379","name":"Pope Benedict XVI"},{"id":"106184866078691","name":"Romário"},{"id":"109601749058079","name":"Pope Benedict XVI"}],"gender":"male","religion":"Cristão - Católico","political":"Foo Party","email":"foobarbaz"
}

Upvotes: 1

Views: 298

Answers (1)

Relaxing In Cyprus
Relaxing In Cyprus

Reputation: 2006

I tested your url on my server and it worked fine:

array
  'cmd' => string 'new_suggestion' (length=14)
  'suggestion' => string 'some suggestion' (length=15)
  'sender' => string '{"id":"1234567890123","name":"Foo Bar Baz","first_name":"Foo","middle_name":"Bar","last_name":"Baz","link":"https://www.facebook.com/foobarbaztest","username":"foobarbaz","birthday":"01/01/1900","hometown":{"id":"1234567890","name":"Test City"},"inspirational_people":[{"id":"111713915507379","name":"Pope Benedict XVI"},{"id":"106184866078691","name":"Romário"},{"id":"109601749058079","name":"Pope Benedict XVI"}],"gender":"male","religion":"Cristão - Católico","political":"Foo Party","email":"foobarbaz@fo'... (length=882)

Is your server configured to support utf-8? Do a phpinfo() and see what you get. I get the following:

HTTP_ACCEPT_CHARSET     ISO-8859-1,utf-8;q=0.7,*;q=0.3

I noticed that the URL contained some odd characters, (Romario, Christaeo etc), perhaps thats an issue without utf-8?

If you need to change http_accept_charset, this may help: http://rackerhacker.com/2007/11/15/change-the-default-apache-character-set/

=== EDIT ===

It turns out, the server was hitting a 512 byte limit for $_GET. Now this varies from web server to web server, which was why it worked fine when we tried it on my server.

Short of upgrading the server the only way around it was to use the jquery .post() instead of .get()

Upvotes: 2

Related Questions