JeffVader
JeffVader

Reputation: 702

JQuery, AJAX, PHP Passing large amounts of data results in error

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

Upvotes: 2

Views: 1598

Answers (2)

JeffVader
JeffVader

Reputation: 702

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.

Upvotes: 2

Mudasir Syed
Mudasir Syed

Reputation: 131

add at the start after php tag hope it works.

ini_set('max_input_vars', 30000);

Upvotes: 0

Related Questions