It is actually not too difficult.
What happens when you enter a URL into the address bar in the browser? If it is very simplistic:
- The browser sends a request to this URL
- The server works off PHP and the browser returns some information (in most cases HTML)
- The browser displays this information on screen.
In the case of AJAX, the overall essence remains the same:
- JavaScript (instead of the address bar) asks the browser to make a request for a certain URL
- The browser sends a request to this URL
- The server works off PHP and the browser returns some information
- Instead of displaying information on the screen, the browser passes it to JavaScript
- All data are under the control of the script. and we can do anything.
In its simplest form, an AJAX request using JQuery looks like this:
$.ajax({ url: 'some url' //URL where request is made }).done(function(dataFromPHP){ //We get here if the request completed successfully //All PHP was put into the browser for a classic appeal //through the browser (text, HTML, anything), //will be read by JavaScript and placed into the variable dataFromPHP. //Next, we can freely operate with these data. //For example, to show using alert alert(dataFromPhp); }).fail(function(){ //Here we find ourselves, if for some reason the request was not successfully alert('failed to query') });
I posted a couple of simple examples of using AJAX on github:
https://github.com/Merzley/ajax-examples. For running nothing but PHP is not required. If something is not clear please contact