I have array made by function .push. In array is very large data. How is the best way send this to PHP script?
   dataString = ??? ; // array?
   $.ajax({
        type: "POST",
        url: "script.php",
        data: dataString, 
        cache: false,
        success: function(){
            alert("OK");
        }
    });
script.php:
  $data = $_POST['data'];
  // here i would like use foreach:
  foreach($data as $d){
     echo $d;
  }
How is the best way for this?
Encode your data string into JSON.
dataString = ??? ; // array?
var jsonString = JSON.stringify(dataString);
   $.ajax({
        type: "POST",
        url: "script.php",
        data: {data : jsonString}, 
        cache: false,
        success: function(){
            alert("OK");
        }
    });
In your PHP
$data = json_decode(stripslashes($_POST['data']));
  // here i would like use foreach:
  foreach($data as $d){
     echo $d;
  }
Note
When you send data via POST, it needs to be as a keyvalue pair.
Thus
data: dataString
is wrong. Instead do:
data: {data:dataString}
 dataString = [];
   $.ajax({
        type: "POST",
        url: "script.php",
        data:{data: $(dataString).serializeArray()}, 
        cache: false,
        success: function(){
            alert("OK");
        }
    });
http://api.jquery.com/serializeArray/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With