| 0 comments ]

Re-Share This Post: Technorati Digg This Stumble Stumble Facebook Twitter

In this article we will be talking about the basic usage of Ajax with jQuery 1.4.2 production (24KB, Minified and Gzipped).


In order to use the JavaScript examples in this article, you should first download jQuery and include it on your page using:
  1. <script type="text/javascript" src="jquery-1.4.2.js"></script>
Throughout this article our Ajax scripts will communicate with ‘serverscript.php‘ our Server script.
  1. <?php
  2. if(isset($_POST['firstname']) &&
  3.    isset($_POST['lastname'])) {
  4.  echo "Hey {$_POST['firstname']} {$_POST['lastname']},
  5.    you rock!\n(repsonse to your POST request)";
  6. }
  7. if(isset($_GET['firstname']) &&
  8.    isset($_GET['lastname'])) {
  9.  echo "Hey {$_GET['firstname']} {$_GET['lastname']},
  10.    you rock!\(response to your GET request)";
  11. }
  12. ?>
Method One - POST (Asynchronous with data):

Transfer data to the server (using POST method), and retrieve a response:
  1. function doAjaxPost() {
  2.  $.ajax({
  3.    type: "POST",
  4.    url: "serverscript.php",
  5.    data: "firstname=clint&lastname=eastwood",
  6.    success: function(resp){
  7.      // we have the response
  8.      alert("Server said:\n '" + resp + "'");
  9.    },
  10.    error: function(e){
  11.      alert('Error: ' + e);
  12.    }
  13.  });
  14. }
  15. doAjaxPost();
Method Two - GET (Asynchronous with data):

Transfer data to the server (using GET method), and retreive a response:
  1. function doAjaxGet() {
  2.  $.ajax({
  3.    type: "GET",
  4.    url: "serverscript.php",
  5.    data: "firstname=clint&lastname=eastwood",
  6.    success: function(resp){
  7.      // we have the response
  8.      alert("Server said:\n '" + resp + "'");
  9.    },
  10.    error: function(e){
  11.      alert('Error: ' + e);
  12.    }
  13.  });
  14. }
  15. doAjaxGet();
Note that GET is the default type for Ajax calls using jQuery, so we really do not need to explicitly state it, but I’ve placed it there just for clarity.

Practical jQuery Example using POST:
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="jquery-1.4.2.js"></script>
  4. </head>
  5. <body>
  6.   <script>
  7. function doAjaxPost() {
  8.  // get the form values
  9.  var field_a = $('#field_a').val();
  10.  var field_b = $('#field_b').val();
  11.  $.ajax({
  12.    type: "POST",
  13.    url: "serverscript.php",
  14.    data: "firstname="+field_a+"&lastname="+field_b,
  15.    success: function(resp){
  16.      // we have the response
  17.      alert("Server said:\n '" + resp + "'");
  18.    },
  19.    error: function(e){
  20.      alert('Error: ' + e);
  21.    }
  22.  });
  23. }
  24. </script>
  25. First Name:
  26. <input type="text" id="field_a" value="Sergio">
  27. Last Name:
  28. <input type="text" id="field_b" value="Leone">
  29. <input type="button" value="Ajax Request" onClick="doAjaxPost()">
  30. </body>
  31. </html>



jQuery Example in Browser

jQuery Example in Browser

Well there you have it, not too tricky and with a weight of only 24Kb for the base library you can’t go wrong. Of course, jQuery can be used for a whole heap of other tasks.

Until next time (when we cover MooTools), Happy A’jaxing!

0 comments

Post a Comment

 
http://www.tsfaenterprise.com TSFA ENTERPRISE TSFA GAS OVEN AND HOME APPLIANCE IN PAKISTAN GAS OVEN GAS OVEN in pakistan Pakistan Home Appliance http://gasovenandhomeappliancesinpakistan.com/ TSFA ENTERPRISE TSFA GAS OVEN AND HOME APPLIANCE IN PAKISTAN GAS OVEN GAS OVEN in pakistan Pakistan Home Appliance