Member Avatar for Member #564028

Is there a way to send a large amount of data through AJAX to a PHP script?

Almost all tutorials say to modify the URL to equal whatever variable your trying to look up. I am wondering if it is possible to pass infomation to the server through an XML or HTML document? Because having a whole array of information in the address bar can be kind of cumbersome.

Dani AI

Generated

@ragnarok511 You do not need to cram arrays into the query string. and are on the right track: send a request body and keep the URL clean. There is no single fixed URL-size limit across browsers or servers, so large payloads belong in the body. For really big data, @diafol’s pattern of sending a file or a server-side reference (an ID) and letting PHP do the heavy lifting is often more reliable.

A simple, modern approach is JSON over the Fetch API. Keep the URL minimal and put your array/object in the body:

const payload = { items: bigArray, note: "whatever else you need" };

const res = await fetch("/page.php", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify(payload)
});
if (!res.ok) throw new Error("HTTP " + res.status);

Fetch is widely supported and designed for request bodies like this. See the Fetch guide on MDN: Using the Fetch API.

When the payload is large, check server limits to avoid mysterious 413/empty $_POST issues:

  • PHP: raise post_max_size (and upload_max_filesize for files). If you submit thousands of form fields, watch max_input_vars. JSON you decode yourself is not counted toward max_input_vars.
  • Web server: increase the request-body cap (for example NGINX client_max_body_size; on Apache, LimitRequestBody).

If you are sending binary or very large data, prefer a file input and multipart/form-data via FormData, or chunk the payload and reassemble server-side. MDN’s FormData doc shows how to build these requests.

Recommended Answers

All 3 Replies

You can send a POST request. That doesn't involve sending data through the URL so the 256KB limit won't apply. If you want to upload a file or something like that, it would be better to just post to and iframe so that the page doesn't refresh.

Member Avatar for Member #120589

It depends what you're trying to pass. You can pass any variable via POST (as mentioned), but I would suggest that you pass the filename of a text file (or XML or anything similar). Let the php script do the hard work of parsing / modifying the text.

Using a library like prototype or jquery may help with "messing with" the ajax object.

You can POST raw data to the server if you needed.

This would be done by setting the correct HTTP headers:

http://www.w3.org/TR/2009/WD-XMLHttpRequest-20091119/#the-setrequestheader-method

Example: send XML data

var client = new XMLHttpRequest();
client.onreadystatechange = function() {
  // do your stuff
  if (this.readyState == 4) alert('sent');
};
client.open('POST', 'page.php', true);
// this controls the content type of the post
client. setRequestHeader('Content-Type', 'text/xml');
// we can send XML since we set content-type to xml
client.send('<user><name>joe</name><age>100</age></user>');

PHP can read the raw content using

$post_data = file_get_contents('php://input');

see: http://www.fijiwebdesign.com/fiji-web-design-blog/acess-the-http-request-headers-and-body-via-php.html

Note my XMLHttpRequest example will only work on W3C compliant implementations. To get an actually working example see:
http://code.google.com/p/xhr/
or
http://code.google.com/p/xmlhttprequest/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.