Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array being chopped off over ajax post. Ajax posting limit?

I have a multidimensional array, which consists of 426 smaller arrays, which also comprise of 4 attributes.. Below is an example of one of 426 arrays...

    array( //Main array
          0 => array( //1 of 426 arrays
               'name' => 'Danny', 
               'email' => '[email protected]', 
               'picture_url' => 'http://www.website.com', 
                'score' => 89
          ),
    )

I'm posting this array with jquery's ajax functions to a php file, which adds them to a database... My problem is that the array seems to be chopped off when it's posted to the php file. Only about half the array actually reaches the php file...

This has led me to believe that there may be a file size limit when posting over ajax. However, the size of my array seems to be relatively small..

I'm running my application on WAMP..

Can anyone shed some light what's possibly happening?

UPDATE:

I'm posting my array like so:

$.ajax({
  type: "POST",
  url: "invite_friends.php",
  data: {
    theID: me.id,
    friends: multidimensional_array //This is the array <---
  },
  success: function(data, textStatus, jqXHR) {
    return console.log(data);
  },
  error: function(jqXHR, textStatus, errorThrown) {
    return alert("Error: Oops, there has been a problem");
  }
});

And I retrieve my array (in invite_friends.php) like so..

if($_POST['friends']) {
    $friends = $_POST['friends'];   
} else {
    $friends = FALSE;
} 
like image 347
Danny Avatar asked Sep 17 '12 15:09

Danny


People also ask

Is there a limit for post in Ajax?

@Bryuk:- It is really difficult to actually tell the cause as there could be many reasons. I would recommend you to use any debugger to check the actual problem but going by your title line there is no such limit for POST in Ajax

What is Ajax post method in jQuery?

JQuery Ajax POST Method Sends an asynchronous http POST request to load data from the server. Its general form is: jQuery.post(url [, data ] [, success ] [, dataType ])

How do I send a POST request in Ajax?

JQuery Ajax POST Method. Sends an asynchronous http POST request to load data from the server. Its general form is: jQuery.post( url data ] [, success ] [, dataType ] ) url : is the only mandatory parameter. This string contains the adress to which to send the request.

What is the use of Ajax?

AJAX stands for Asynchronous JavaScript And XML, which allows the webpage to be updated in the backgroud without refreshing the page. Using AJAX you can either request, receive or send the data to server. Its a general convention to use the POST method to send the data to server & server creates new resources received in the request body.


2 Answers

You need to open your php.ini file and set (or create) this line:

max_input_vars = 1000000

max_input_vars has a default value of 1000, which will cut off an array at 1000 total elements. Just change it to a really high number (in my case, I needed to set it to one million).

From the PHP Manual:

How many input variables may be accepted (limit is applied to $_GET, $_POST and $_COOKIE superglobal separately). Use of this directive mitigates the possibility of denial of service attacks which use hash collisions. If there are more input variables than specified by this directive, an E_WARNING is issued, and further input variables are truncated from the request. This limit applies only to each nesting level of a multi-dimensional input array.

Keep in mind: As the manual says, this default limit was put in place to prevent denial of service attacks.

Hope this helps even though this is an old question.

like image 80
FastTrack Avatar answered Sep 17 '22 15:09

FastTrack


You should convert all your data in json format at the client side and send it as a text.

var jsonDataString = JSON.stringify(data)
//data - array, associative array, any other variables

$.ajax({
...
    data: {
        friends: jsonDataString,
    }
...
});

At the server side just decode json file.

<?php
...
$friendsJsonString = $_POST['friends'];
$friends = json_decode($friendsJsonString);
...

Changing of php.ini is not a good idea because of security risks.

like image 27
Andrew Avatar answered Sep 18 '22 15:09

Andrew