2

I'm trying to pass quite a bit of data from a page using AJAX to a PHP script. This works fine for some small amounts of data, but fails with the following error when there is more data.

"PHP Warning:  Unknown: Input variables exceeded 1000. To increase the limit change max_input_vars in php.ini. in Unknown on line 0,"

The JQuery I'm using is:

var data = $flowchart.flowchart('getData');

$.ajax({
    type: "post", 
    cache: false,
    data: data,
    url: "test.php?name=" +  encodeURIComponent(name) + "&action=" + action,
    success: function(data) {
        console.log (data)
    }
}); 

data is effectively a json string of data.

eg:
sites: 0: {fromOperator: "SiteA", fromConnector: "output_1", fromSubConnector: "0", toOperator: "location1", toConnector: "input_1", …} 1: {fromOperator: "SiteA", fromConnector: "output_2", fromSubConnector: "0", toOperator: "location2", toConnector: "input_1", …} 2: {fromOperator: "start", fromConnector: "output_1", fromSubConnector: "0", toOperator: "SiteA", toConnector: "input_1", …}

and is read in PHP simply using:

// Values from data.
$sites = (isset($_POST['sites']) && count($_POST['sites']) > 0) ? $_POST['sites'] : NULL;
$staff = (isset($_POST['staff']) && count($_POST['staff']) > 0) ? $_POST['staff'] : NULL;

How can I pass large amounts of data link this but still reference it in PHP using $_POST['sites'] & $_POST['staff'] etc ?

I thought I'd need to do something with JSON, but I can work out what.

Thanks

3
  • Usually PHP specifies a size limit on all requests, what is the post_max_size set in you php.ini file? Commented Nov 19, 2019 at 10:18
  • 1
    max_input_vars is 1000, post_max_size is 8MB Commented Nov 19, 2019 at 10:26
  • Well, it tells you the exact problem and even the solution. You send more than 1000 fields which is the limit, to be able to work with more you need to increase it. Commented Nov 19, 2019 at 13:28

2 Answers 2

2

I've got this working by applying JSON.stringify to data before sending it:

data = encodeURIComponent(JSON.stringify(data))

$.ajax({
    type: "post", 
    cache: false,
    data: 'data=' + data,
    url: "test.php?name=" +  encodeURIComponent(name) + "&action=" + action,
    success: function(data) {
        console.log (data)
    }
}); 

Then in my PHP using:

$js= json_decode($_POST['data'], true);
$staff= (isset($js['sites']) && count($js['sites']) > 0) ? $js['sites'] : NULL;
$staff= (isset($js['staff']) && count($js['staff']) > 0) ? $js['staff'] : NULL;

This seems to work as before but is allowing the larger data.

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

Comments

0

add at the start after php tag hope it works.

ini_set('max_input_vars', 30000);

3 Comments

Thanks. I'd expect that to work.. but I don't know what the maximum limit I will need is. Is there a better way to send the data so I don't get this issue ?
add -1 in limit i think -1 works for infinite limit
Didn't work.. max_input_vars has a changeable mode of PHP_INI_PERDIR meaning it can't be changed using ini_set

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.