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.

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:

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.