I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use.
Why should I use jQuery as opposed to regular JavaScript?
Now I know what you may be thinking... why load a whole JavaScript library/framework, just to do a simple AJAX request?
My answer: There is no reason. Don't.
I believe that you should only use a library like jQuery if you are solving some complex problems and/or you want to use some of the vast custom plug-ins that are available with some libraries.
Should I host the jQuery library or let Google do it?
There are two ways you can include the jQuery library.
Download from jQuery's site and store it on your server
In this example I will be using PHP, but since jQuery can be used with just about all web languages - you can use any of them you would like. I am also going to make this article really really simple, so don't expect deep jQuery functionality!
First Include jquery.js (Again, I am using Google)
Finally, here is the somepage.php that is nested within the jQuery function as seen above. Because the variables that I pass this page are sent via POST, my PHP program on the host I can access those variables via:
Remember, somepage.php doesn't have to be PHP -- it can be any page you can access on the web.
[Ed. Note:] ...subject, of course, to cross-domain browser security policies. That is, the target page typically must be hosted on the same domain and subdomain as the original page that issues the jQuery AJAX requests.
Final Thoughts
Now that you have a basic example and can see how simple it can be to use jQuery's AJAX functions, I encourage you to continue to learn this wonderful library.
I plan to post more articles on using jQuery to solve very simple to very complex problems in the near future. I thank you for reading this article and happy jQuery-ing!
Comments (3)
Commented:
Commented:
Commented: