Home >Backend Development >PHP Tutorial >How to Send Large Arrays to a PHP Script via Ajax?
Sending Array to PHP Script via Ajax
When dealing with large array data, transferring it to a PHP script through Ajax requires careful consideration.
Best Practice: JSON Encoding
To handle large arrays efficiently, it is recommended to encode the data into JSON (JavaScript Object Notation). JSON provides a structured and compact representation that is easily parsable by both JavaScript and PHP.
Ajax Request
The updated Ajax request would then appear as follows:
dataString = ??? ; // array? var jsonString = JSON.stringify(dataString); $.ajax({ type: "POST", url: "script.php", data: {data: jsonString}, cache: false, success: function(){ alert("OK"); } });
PHP Script
In the PHP script, the data can be decoded using json_decode as follows:
$data = json_decode(stripslashes($_POST['data'])); foreach($data as $d){ echo $d; }
Additional Notes
The above is the detailed content of How to Send Large Arrays to a PHP Script via Ajax?. For more information, please follow other related articles on the PHP Chinese website!