Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Send array with Ajax to PHP script
  2. dataString = ??? ; // array?
  3.    $.ajax({
  4.         type: "POST",
  5.         url: "script.php",
  6.         data: dataString,
  7.         cache: false,
  8.  
  9.         success: function(){
  10.             alert("OK");
  11.         }
  12.     });
  13.        
  14. $data = $_POST['data'];
  15.  
  16.   // here i would like use foreach:
  17.  
  18.   foreach($data as $d){
  19.      echo $d;
  20.   }
  21.        
  22. dataString = ??? ; // array?
  23. var jsonString = JSON.stringify(dataString);
  24.    $.ajax({
  25.         type: "POST",
  26.         url: "script.php",
  27.         data: {data : jsonString},
  28.         cache: false,
  29.  
  30.         success: function(){
  31.             alert("OK");
  32.         }
  33.     });
  34.        
  35. $data = json_decode(stripslashes($_POST['data']));
  36.  
  37.   // here i would like use foreach:
  38.  
  39.   foreach($data as $d){
  40.      echo $d;
  41.   }
  42.        
  43. dataString = [];
  44.    $.ajax({
  45.         type: "POST",
  46.         url: "script.php",
  47.         data:{data: $(dataString).serializeArray()},
  48.         cache: false,
  49.  
  50.         success: function(){
  51.             alert("OK");
  52.         }
  53.     });
  54.        
  55. $data = explode(",", $_POST['data']);
  56. foreach($data as $d){
  57.      echo $d;
  58. }