Jump to content

How to use Ajax to retrieve data from PHP file without jQuery


Recommended Posts

This is a basic tutorial on how to retrieve information from a php parser using Ajax without jQuery.


This is very useful when you wish to check if a user exists, check for live chat messages, etc.




function send(){
var hr = new XMLHttpRequest(); //this variable represents your ajax request
var url = "parser.php"; //the url to your php parser
hr.open("POST", url, true); //initiate the ajax request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //set the request header
hr.onreadystatechange = function(){
if(hr.readyState == 4 && hr..status == 200){ //this code checks whether or not the request has fully processed and returned valid
var text = hr.responseText; //this is the text sent back from the php file
alert(text); //you can do whatever you wish with the response text


PHP (short example)


echo "this text will be retrieved using Ajax";

Edited by Nathan
Link to comment
Share on other sites

Great thanks, added the code tags in there.


So what's the advantage of doing it this way?


There's not really a specific advantage of doing it this way vs using jQuery. It's the original way I learned it though so I tend to use it a lot since it's what I'm used to. It's also good just see exactly what's really going on when you use jQuery. A behind the scenes if you will. It's also good to know in case you don't have access to jQuery for some reason.

Link to comment
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.

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.

  • Create New...