- Community Pick
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
- Host it externally through via Google's jQuery stored library
You will see me using the Google-hosted library for the few reasons explained here:
http://encosia.com/2008/1
Enough of the talking, lets get to coding!
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)
Next is the jQuery function that will execute the request
Lastly (on this page) we will add the div that will reveal our AJAX results and the link that will execute the function.
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:
$_POST[ 'var_name' ];
That is it! Here it is all together:
Remember, somepage.php doesn't have to be PHP -- it can be any page you can access on the web.
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.
http://api.jquery.com/cate
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!
by: dineshfdr on 2010-08-02 at 06:55:50ID: 17726