Best strategy on sending an array with AJAX

Asked
Active3 hr before
Viewed126 times

7 Answers

arraysending
90%

PHP accesses the GET string, allowing you to reference it as an array,PHP accesses the GET string (or POST data), allowing you to reference it as an array,If you send the data by post it should be converted to an array by most languages.,The Contents are licensed under creative commons.

For example, submitting this :

<input type="checkbox" class="linkStyle" name="style[]" value="1" checked="checked" /> 1
<input type="checkbox" class="linkStyle" name="style[]" value="2" checked="checked" /> 2
<input type="checkbox" class="linkStyle" name="style[]" value="3" checked="checked" /> 3

i'll send to the server these parameters :

style[] = 1
style[] = 2
style[] = 3

Referring to the example before, it will send somethings like :

style: style % 5 B % 5 D = Style1 & style % 5 B % 5 D = Style2 & style % 5 B % 5 D = Style3

and this is uncomfortable on PHP, because it doesnt create an array for this values. So i've done my own script that emulate this behaviour. such as :

var styles = "";
$('.linkStyle:checked').each(function(i, el) {
   styles = styles + 'style[]=' + $(this).val() + '&';
});

$.ajax({
   url: "list/browse_ajax.php",
   type: "POST",
   data: styles + '&id=browseUpdate&actualArtist=' + browseArtist + '&actualEvent=' + browseEvent + '&actualDate=' + browseData,
   success: function(data) {
      $('#browseList').html(data);
   },
   error: function(data) {
      $('#browseList').html("Error");
   }
});
load more v
88%

If you have been trying to send a one dimentional array and jquery was converting it to comma separated values >:( then follow the code below and an actual array will be submitted to php and not all the comma separated bull**it.,Pardon my language, but there are hell lot of Rube-Goldberg solutions scattered all over the web and specially on SO, but none of them are elegant or solve the problem of actually posting a one dimensional array to php via ajax post. Don't forget to spread this solution.,I have array made by function .push. In array is very large data. How is the best way send this to PHP script?,Data in jQuery ajax() function accepts anonymous objects as its input, see documentation. So example of what you're looking for is:

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;
}
load more v
72%

$.ajax({
   type: "POST",
   url: "submit",
   data: ({
      detailsArr: detailsArr
   }),
   success: function(html) {
      alert("Submitted");
   }
});
load more v
65%

What is 'CodeProject'?, help? What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum About Us ,If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.,articles Browse Topics> Latest Articles Top Articles Posting/Update Guidelines Article Help Forum Submit an article or tip Import GitHub Project Import your Blog

$.ajax({
   type: "POST",
   url: "/Emp/MasterDetails",
   data: JSON.stringify(Details),
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   traditional: true,
   success: function(data) {
      alert(data.massge + " record(s) inserted.");
   }
});
load more v
75%

I have array made by function .push. In array is very large data. How is the best way send this to PHP script?,How is the best way for this?,dataString suggests the data is formatted in a string (and maybe delimted by a character).,if dataString is not a string but infact an array (what your question indicates) use JSON.

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;
  }
load more v
40%

I have array made by function .push. In array is very large data. How is the best way send this to PHP script?,How is the best way for this?,and now put all your contact array and other arrays into it, right you loop through them as described above:,now this is your AsynchTask to send them to your PHP server:

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;
  }
load more v
22%

Traditionally, a web page has to send a request to the server to receive new data; that is, the page requests data from the server. With server-sent events, it's possible for a server to send new data to a web page at any time, by pushing messages to the web page. These incoming messages can be treated as Events + data inside the web page. See also: Using server-sent events.,XPath stands for XML Path Language, it uses a non-XML syntax that provides a flexible way of addressing (pointing to) different parts of an XML document. As well as this, it can also be used to test addressed nodes within a document to determine whether they match a pattern or not.,The Extensible Markup Language (XML) is a W3C-recommended general-purpose markup language for creating special-purpose markup languages. It is a simplified subset of SGML, capable of describing many different kinds of data. Its primary purpose is to facilitate the sharing of data across different systems, particularly systems connected via the Internet.,The Fetch API provides an interface for fetching resources. It will seem familiar to anyone who has used XMLHTTPRequest, but this API provides a more powerful and flexible feature set.

load more v

Other "array-sending" queries related to "Best strategy on sending an array with AJAX"