KenBrace Posted February 19, 2015 Posted February 19, 2015 (edited) 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. Javascript <script> 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 } } hr.send(); } </script> PHP (short example) <?php echo "this text will be retrieved using Ajax"; ?> Edited February 19, 2015 by Nathan Nathan 1 Quote
Administrators Nathan Posted February 19, 2015 Administrators Posted February 19, 2015 Great thanks, added the code tags in there. So what's the advantage of doing it this way? Quote
KenBrace Posted February 20, 2015 Author Posted February 20, 2015 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.