Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 27 down vote accepted

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}

share|improve this answer
5  
Why use JSON.stringify? What's wrong with just data: {data: dataString}? –  Rocket Hazmat Jul 12 '12 at 15:54
    
@Rocket: I suppose you could do that as well in which case Jquery would serialize the array. –  xbonez Jul 12 '12 at 16:59
4  
Then you wouldn't need json_decode, $_POST['data'] would already be an array. –  Rocket Hazmat Jul 12 '12 at 17:01
    
Friendly reminder to always validate your post values throwing them around. –  citizen conn Oct 21 '13 at 1:55

This is a link that might usefull for you Posting Array to PHP using ajax and agree with Vyktor i've tried it and works great,

share|improve this answer
3  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  Szymon Jan 6 at 4:04

dataString suggests the data is formatted in a string (and maybe delimted by a character).

$data = explode(",", $_POST['data']);
foreach($data as $d){
     echo $d;
}

if dataString is not a string but infact an array (what your question indicates) use JSON.

share|improve this answer
    
dataString this is only example. I dont must use this, but i dont know what i must use –  Paul Attuck Jan 25 '12 at 11:04
    
data: { id_arr: [array variable goes here]}, will be accessible as $_POST['id_arr'] as an array in PHP –  bromelio Jun 24 at 14:37

Data in jQuery ajax() function accepts anonymous objects as its input, see documentation. So example of what you're looking for is:

dataString = {key: 'val', key2: 'val2'};
$.ajax({
        type: "POST",
        url: "script.php",
        data: dataString, 
        cache: false,

        success: function(){
            alert("OK");
        }
    });

You may also write POST/GET query on your own, like key=val&key2=val2, but you'd have to handle escaping yourself which is impractical.

share|improve this answer
    
This didn't work for me, however data: { id_arr: [array variable goes here]} will be accessible as array $_POST['id_arr'] in PHP –  bromelio Jun 24 at 14:38
 dataString = [];
   $.ajax({
        type: "POST",
        url: "script.php",
        data:{data: $(dataString).serializeArray()}, 
        cache: false,

        success: function(){
            alert("OK");
        }
    });

http://api.jquery.com/serializeArray/

share|improve this answer
    
and how can i next read this in PHP file? –  Paul Attuck Jan 25 '12 at 11:02
1  
Since serializeArray works by creating a JSOn string, you would first need to use json_decode on the PHP side –  xbonez Jan 25 '12 at 11:34
2  
I'd suggsst dropping serializeArray, and just doing data: {data: dataString}. Then $_POST['data'] will be an array. –  Rocket Hazmat Jul 12 '12 at 15:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.