Member Avatar for ragnarok511

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.

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 diafol

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:

Example: send XML data

var client = new XMLHttpRequest();
client.onreadystatechange = function() {
  // do your stuff
  if (this.readyState == 4) alert('sent');
};'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

PHP can read the raw content using

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


Note my XMLHttpRequest example will only work on W3C compliant implementations. To get an actually working example see:

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.