1

I have problem to parse a get variable which contains an array and send it with jquery ajax.

the array what I send can look as it follows

ids%5B%5D   1403172219
ids%5B%5D   1530542001
ids%5B%5D   1582588379

but when I try to dump($_GET['ids]) I get null I dumb the hole $_GET than looks like

ids%5B%5D] => 1530542001 

JQuery

function update_category(selected) {
    console.log(cids);
    $.ajax({
        url: '/admin/',
        type: 'GET',
        dataType: "application/JSON",
        data: {
            ids: cids,
            s_category: selected,
            controller: 'products',
            traditional: true,
            action: 'update_category'
        },
        success: function(data) {
            addAlert('alert-'+data, data);
        },
        error: function(data) {
            addAlert('alert-'+data.responseText, data.responseText);
        }
    });

controller

  protected function update_category() {
        print_r($_GET);
        $cat_ids = $_GET['ids'];
        $category = $_GET['s_category'];       
        if(!empty($cat_ids) && !empty($category)){
            $this->model->updateCategory($cat_ids, $category);
        } else {
            echo 'Fähler in der Daten übergabe!';
            exit();
        }              
    }
}

on localhost I use PHP 5.4 and I do not face any problems on live Server ubuntu PHP 5.3.10 the error persist

the correct output on localhost

Array ( 
[ids] => Array (
    [0] => B6Q09EA#ABD 
    [1] => H4P04EA#ABD 
    ) 
[s_category] => 4
[controller] => products 
[traditional] => true 
[action] => update_category 
) 

ids are builded like

cids = [];
jQuery.each(sData, function(key, value) {
       cids.push(value.value);
});
9
  • it will only show last one because it will replace all and equal to last one please change index aur make ids[] as an array Commented Jan 29, 2014 at 11:50
  • What is the problem? This behaviour is expected. Please clarify what you want to do. And show us your jquery code. Commented Jan 29, 2014 at 11:50
  • Is the character encoding the same on both machines? Also, is this output printing on SO correctly? Commented Jan 29, 2014 at 11:55
  • what you mean my meta header? Commented Jan 29, 2014 at 11:58
  • Seems your AJAX is sending a JSON encoded string while your PHP is expecting an unencoded GET variable? Commented Jan 29, 2014 at 12:48

1 Answer 1

2
+50

Your live server will probably be an IPv6 enabled host. That means that [ and ] (or %5B and %5D) are reserved characters. encodeURI, which is applied to your stringified request string, doesn't, or rather cannot encode these chars properly, just as & and = aren't encoded.
The issue here is the PHP version (or maybe suhosin) you have installed on your server, I think. I make this assumption based on this bug report, where one response stated the following:

I just updated from PHP 5.3 (with suhosin 0.9.33) to PHP 5.5 (without suhosin) and this fixed the issue. I guess some security stuff within suhosin might be the cause for the spinning icon issue I had.

I could switch back to PHP 5.3 and remove the suhosin module to further investigate this, but I think the public interest might not be too high as I seem to be the only reported case with this problem. :-)

verify if your server has suhosin installed, and check your phpinfo() on both servers, that should fix it.
Alternatively, you could choose to not use jQuery for this, and write your own XMLHttpRequest, and add a encodeURI(stringifiedObject).replace(/%5B/g, '[').replace(/%5D/g,']'); before sending the data.

Some more details on the IPv6 thing: on MDN

A temporary workaround/fix might be to fix the request server-side, if you're not willing to ditch jQ:

$post = http_get_request_body();//requires http extension
$post = str_replace(
    array('%5B', %5D'),
    array('[',']'),
    $post;
);

Then using parse_str, you can construct the $_POST array:

parse_str($post, $_POST);

That should give you the array you need.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your feedback I will check it Monday. I hope this is the workaround
my phpinfo on live server shows that I'm on IPv6 I Have been disabling IPv6 support but phpinfo shows the same
@fefe: and what about suhosin? is that being used? and is it working using http_get_request_body, instead of using the flawed $_POST array?
okay I just made an upgrade to php5.4, suhosin is not listed in php info but the problem still persist. Because of the complexity of my jquery ajax I would like to avoid to refactor
@fefe: on suhosin, you may want to look at this, it needn't always show up in phpinfo. The whole idea of this extension is security, so it may be there, but hidden from the phpinfo
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.