Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
thinkus

Passing Ajax Data To Php With Javascript

Recommended Posts

Hi Larry,
 
Great fan of you books! I've recently gone over Chapter 11 and I had a question regarding passing data from JSON to PHP using the code provided in the book for test.js (434-436).  I've modified the script to use JSON and that works beautifully.  I'd like to know if there is a clean JS solution to pass data to PHP without relying on jQuery.ajax({ data })?  
 
I've looked everywhere for a solution but the solution seems easily resolved with jQuery, but I'd like to avoid that if possible.
 
Here is the code I've used as dictated in the book:

 

window.onload = function() {
	'use strict';
	var ajax = getXMLHttpRequestObject();
	
	ajax.onreadystatechange = function() {
		if ( ajax.readyState == 4 ) {
			if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) {
				var data = JSON.parse(ajax.responseText);
				var file = '';
				file += 'Original: ' + data['org'].file + '<br>';
				file += 'Processed: ' + data['pre'].file + '<br>';
				document.getElementById('output').innerHTML = file;	
			} else {
				document.getElementById('output').innerHTML = 'Error: ' + ajax.statusText;
			}
		}
	};
	
	document.getElementById('btn').onclick = function() {
		ajax.open('POST', 'resources/test.json', true);
		ajax.setRequestHeader('Content-Type', 'application/json');
		ajax.send(null);
	};
};

 

I would like to be able to echo out the data['org'].file and data['pre'].file in PHP after the request is made.  Any guidance or suggesting would be appreciated.

Share this post


Link to post
Share on other sites
thinkus, welcome to the forums.

 

I may be misunderstanding your question, but my understanding is that you want to send a JSON request to a PHP script without using jQuery, right?

 

Probably the easiest solution would be to use the JSON.stringify method, which is the compliment of the JSON.parse method; it turns a JS object into a string.

By doing so, you can send the "stringified" JS object to a PHP script as a string and process it as necessary. (You would then likely want to use the json_decode function on the PHP side to turn the string back into an object for easy processing.)

 

If you go the above route, then all you need to do is make a standard PHP script request using Ajax. You can then receive the stringified JS object on the PHP side using the $_GET superglobal. For example, you could make the following Ajax request:

document.getElementById('btn').onclick = function() {
  ajax.open('GET', 'resources/PHP-script-name.php?json_string=' + encodeURIComponent(JSON.stringify(test.json)), true);
  ajax.send(null);
};

 

Note that for this example, I used the GET method, but you can just as easily switch it to use the POST method (with some minor changes to the code).

If you use the above code, then your JSON string will be accessible on the PHP through the following variable:

$_GET['json_string']

 

The other option would be to pick out the necessary data from the JS object, and send them as separate pieces of data.

Just as an example, if test.json contained "id" and "name" properties, then you might do the following instead:

document.getElementById('btn').onclick = function() {
  ajax.open('GET', 'resources/PHP-script-name.php?id=' + encodeURIComponent(test.json.id) + '&name=' + encodeURIComponent(test.json.name), true);
  ajax.send(null);
};

 

Anyway, I suppose my point after that long post is that there are any number of solutions.

It really just depends how you want to do things and which way fits your specific situation best.

Does that answer your question?
  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...