Jquery Course

jQuery get() and post() are aplications of the ajax() method.

get() and post() functions

$.get() and $.post() are simple wrapper functions around the $.ajax method, they are used generaly to send and receive data from the server.
These two functions are almost identical, with the only difference being the HTTP request type: $.get will perform a GET request and $.post will perform a POST request.
Syntax:
$.get(url, data, success, dataType);

$.post(url, data, success, dataType);
- url - specifies the url to send the request to.
- data - (optional) contains data that needs to be sent to the server.
- success - (optional) a function to run if the request succeeds function(response, status, xhr):
        • response - contains the result data from the request (the response from the server).
        • status - contains the status of the request.
        • xhr - contains the XMLHttpRequest object.
- dataType - specifies the data type that will be passed on to the success function; this can be xml, html, script, json, jsonp, or text.

Example with get()

When a specific button is clicked, sends some data to a PHP script and display the response in a DIV element using an AJAX GET request:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery Ajax - GET</title>
<script type="text/javascript" src="jquery_1.6.1.js"></script>
<script type="text/javascript"><!--
$(document).ready(function() {
  $('#buton').click(function() {
    var data = 'id=an_id';
    $.get('script.php', data, function(response){
      $('#dv').html(response);
    });
  });
});
--></script>
</head>
<body>
<div id="dv">Here will be displayed the response.</div><br />
<button id="buton">Click</button>
</body>
</html>
Demo:
Here will be displayed the response.


Example with post()

When a form is submited, gets the value of an input text box, sends it to a PHP script and display the response in a DIV element using an AJAX POST request:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery Ajax - POST</title>
<script type="text/javascript" src="jquery_1.6.1.js"></script>
<script type="text/javascript"><!--
$(document).ready(function() {
  $('form').submit(function() {
    var data = 'name='+$('#nm').val();
    $.post('script.php', data, function(response){
      $('#dv').html(response);
    });

    return false;      // required to not open the page when form is submited
  });
});
--></script>
</head>
<body>
<div id="dv">Here will be displayed the response.</div><br />
<form action="script.php" method="post">
 Name: <input type="text" name="nm" id="nm" /><br />
 <input type="submit" value="Submit" />
</form>
</body>
</html>
Demo:
Here will be displayed the response.

Name:

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which type of <input> creates a date input control, such as a pop-up calendar?
type="text" type="date" type="button"
<input type="date" name="set_date" value="2012-10-15" />
Which CSS property adds shadow effects to the text of an element?
font-style color text-shadow
h2 {
  text-shadow: 2px 3px 3px #a0a1fe;
}
Click on the function that adds new elements to the end of an array.
pop() shift() push()
var pags = ["lessons", "courses"];
pags.push("download", "tutorials");
alert(pags[2]);            // download
Which function sorts an array by key, in ascending order, maintaining key to data correlations?
asort() ksort() sort()
$lang =[10=>"PHP", 20=>"JavaScript", "site"=>"coursesweb.net");
ksort($lang);
var_export($lang);     // array ("site"=>"coursesweb.net", 10=>"PHP", 20=>"JavaScript")

S H A R E

Sharing Twitter

Last accessed pages

  1. Get and Modify content of an Iframe (32406)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142946)
  3. Simple PHP Upload Script (8206)
  4. Script comments with pagination (4453)
  5. Upload Script for Gallery of Images and Audio files (9763)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (179)
  2. The Mastery of Love (19)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (18)
  4. CSS cursor property - Custom Cursors (16)
  5. Read Excel file data in PHP - PhpExcelReader (13)