Simple AJAX example using jQuery

AID: 3508
  • Status: Published

3450 points

  • Bygalexander07
  • TypeTutorial
  • Posted on2010-07-29 at 21:22:20
Awards
  • Community Pick
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.


You will see me using the Google-hosted library for the few reasons explained here:
http://encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/

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)
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
                                    
1:

Select allOpen in new window



Next is the jQuery function that will execute the request
<script type="text/javascript">
function ajax_request(first_name,last_name){
   $.post("somepage.php", { first_name: ""+first_name+"", last_name: ""+last_name+"" },
       function(data){
           $("#return_data_div").html(data);
   }); 
}
</script>
                                    
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window



Lastly (on this page) we will add the div that will reveal our AJAX results and the link that will execute the function.
<a href="javascript:ajax_request('Greg','Alexander');">Who wrote this article on experts-exchange.com?</a>
<div id="return_data_div"></div>
                                    
1:
2:

Select allOpen in new window



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' ];
<?php
echo $_POST['first_name']." ".$_POST['last_name'];
?>
                                    
1:
2:
3:

Select allOpen in new window



That is it!  Here it is all together:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>

<script type="text/javascript">
function ajax_request(first_name,last_name){
   $.post("somepage.php", { first_name: ""+first_name+"", last_name: ""+last_name+"" },
       function(data){
           $("#return_data_div").html(data);
   }); 
}
</script>

<a href="javascript:ajax_request('Greg','Alexander');">Who wrote this article on experts-exchange.com?</a>
<div id="return_data_div"></div>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window



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.

    http://api.jquery.com/category/ajax/

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!
Asked On
2010-07-29 at 21:22:20ID3508
Tags

AJAX

,

jQuery

Topic

Jquery

Views
1747

Comments

Expert Comment

by: dineshfdr on 2010-08-02 at 06:55:50ID: 17726

Keep it up..Dude

Expert Comment

by: joaochagas on 2012-01-28 at 10:15:46ID: 34834

Congratulations! Excellent article.

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Jquery Experts

  1. leakim971

    144,463

    Master

    0 points yesterday

    Profile
    Rank: Genius
  2. Proculopsis

    51,000

    Master

    0 points yesterday

    Profile
    Rank: Sage
  3. mplungjan

    50,388

    Master

    0 points yesterday

    Profile
    Rank: Savant
  4. BuggyCoder

    29,968

    0 points yesterday

    Profile
    Rank: Sage
  5. basicinstinct

    29,004

    0 points yesterday

    Profile
    Rank: Genius
  6. chaituu

    27,436

    0 points yesterday

    Profile
    Rank: Sage
  7. JonNorman

    27,008

    200 points yesterday

    Profile
    Rank: Master
  8. Kravimir

    23,701

    0 points yesterday

    Profile
    Rank: Genius
  9. hielo

    22,150

    0 points yesterday

    Profile
    Rank: Savant
  10. tommyBoy

    16,864

    0 points yesterday

    Profile
    Rank: Genius
  11. sedgwick

    14,400

    0 points yesterday

    Profile
    Rank: Genius
  12. stevepwales

    14,200

    0 points yesterday

    Profile
    Rank: Master
  13. designatedinitializer

    14,000

    0 points yesterday

    Profile
    Rank: Master
  14. LZ1

    13,800

    0 points yesterday

    Profile
    Rank: Genius
  15. kozaiwaniec

    13,300

    1,500 points yesterday

    Profile
    Rank: Guru
  16. COBOLdinosaur

    11,500

    0 points yesterday

    Profile
    Rank: Genius
  17. anuradhay

    11,068

    0 points yesterday

    Profile
    Rank: Guru
  18. StingRaY

    11,000

    0 points yesterday

    Profile
    Rank: Wizard
  19. atique_ansari

    10,902

    0 points yesterday

    Profile
    Rank: Master
  20. mrh14852

    10,900

    0 points yesterday

    Profile
    Rank: Master
  21. alex_code

    10,250

    0 points yesterday

    Profile
    Rank: Guru
  22. gurvinder372

    9,800

    0 points yesterday

    Profile
    Rank: Genius
  23. _agx_

    9,600

    0 points yesterday

    Profile
    Rank: Genius
  24. nap0leon

    9,400

    0 points yesterday

    Profile
    Rank: Sage
  25. elvin66

    8,200

    1,800 points yesterday

    Profile
    Rank: Sage

Hall Of Fame