Ajax Tutorial
- What is Ajax
- How Ajax Work
- ajax() and load() Function
- AJAX get() and post() Methods
- AJAX Function
- Ajax Success
- Ajax Button Click
- Ajax div load on button click
- Insert Data
- Delete Data
- User Registration
- ContactUs Form
- Check existing Password in Ajax and change the Password
- Ajax Search
- Ajax Serialize Insert
- Ajax without Searialize Insert
- Ajax User Check Availability
- Ajax OptionBox
- Ajax Dropdown Session
- dynamic text box Add/Remove
- Star Rating
- Pin Check Available
- Ajax Programs
- Clear Form Field after submitting form
- Loader load show / hide
- Multiple Functions
Customer Say
What AJAX get() and post() Methods ?
The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request.
- GET - Requests data from a specified resource.
- POST - Submits data to be processed to a specified resource.
- GET is basically used for just getting (retrieving) some data from the server.
Note: The GET method may return cached data. - POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.
Example :- Change the text of a element using an AJAX request:
File Name:- demo.txt
<html> <head> <title>The jQuery Result
Example</title>
</head>
<body>
<h2>THIS IS RESULT...</h2>
<p id="p1">
This is some text in a paragraph. </p>
<div
id="div1" > </div >
</body>
</html>
File name : index.php
<html> <head> <title>The jQuery
Example</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<
script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#btn").click(function(){
$.ajax({
url:"demo.txt",
success:function(result){
$("#div1").html(result);}})
}); });
</script>
</head>
<body>
</body>
</html>
GET Request method using jQuery
Syntax:
$.get(URL,callback);
OR
$.get( url, [data], [callback], [type] )
The first parameter of $.get() is the URL we wish to request ("demo_test.php").
The second parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
The $.get() method requests data from the server with an HTTP GET request. It allows a single callback function to be specified that will be executed when the request is complete (and only if the response has a successful response code).
jQuery.get( url, [data], [callback], [type] )
- url: (String) The URL of the page to load. A string containing the URL to which the request is sent.
- data (Optional): it represents key/value pairs that will be sent to the server.
- callback (Optional): A function to be executed whenever the data is loaded successfully.
- type (Optional): (String) Type of data to be returned to callback function: "xml", "html", "script", "json", "jsonp", or "text".
File name : demo_test.php
<?php
//echo "hi mahi";
$fname = "mahtab";
$lname = "habib";
echo "First Name =" .$fname;
echo "\n";
echo "Last Name =" .$lname;
?>
File name : index.php
<html>
<head>
<title>The jQuery Example</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("button").click(function(){
$.get("demo_test.php",
function(data, status){
alert("Data: " + data + "\n Status: " + status);
});
});
});
</script>
</head>
<body>
<button>GET request</button>
</body>
</html>
Example 2 :-
File name : index.php
<html>
<head>
<title>The jQuery Example</title>
<script type = "text/javascript"
src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("#btn").click(function(event){
$.get( "info.php",
//{ name: "Zara" },
function(data) {
$('#pos').html(data);
}
);
});
});
</script>
</head>
<body>
<p>Click on the button to load info.php file −</p>
<span id = "pos" style = "background-color:#cc0;">
STAGE
</span>
<div><input type = "button" id = "btn"
value = "Show Data" /></div>
</body>
</html>
File name : info.php
<?php
$fname = "mahtab";
$lname = "habib";
echo "First Name =" .$fname;
echo "\n";
echo "Last Name =" .$lname;
?>
jQuery $.post() Method
The $.post() method requests data from the server using an HTTP POST request.
Syntax:
$.post(URL,data,callback);
The required URL parameter specifies the URL you wish to request.
The optional data parameter specifies some data to send along with the request.
The optional callback parameter is the name of a function to be executed if the request succeeds.
Example :-
File name : index.php
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("info.php",
//{name:"mahi", city:"Delhi"},
function(data,status){
alert("Data: " + data + "\n Status: " + status);
});
});
});
</script>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
</body>
</html>
File name : info.php
<?php
$fname = "mahtab";
$lname = "habib";
echo "First Name =" .$fname;
echo "\n";
echo "Last Name =" .$lname;
?>
The first parameter of $.post() is the URL we wish to request ("info.php").
Then we pass in some data to send along with the request (name and city).
The Php script in "info.php" reads the parameters, processes them, and returns a result.
The third parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
File name : index.php
<html> <head>
<title>The jQuery Example</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" language="javascript"> $(document).ready(function() { $("#btn").click(function(event)
{
$.post( "result.php",
// { name: "Zara" },
function(data)
{
$('#showdiv').html(data);
} ); }); });
</script> </head>
<body> <p>Click on the button to load result.html file −</p>
<div id="showdiv" style="background-color:cc0;"> STAGE </div>
<input type="button" id="btn" value="Load Data" />
</body>
</html>
Example
File name : result.php
<?php
$fname = "mahtab";
echo "First Name =" .$fname;
?>
jQuery Ajax Post Data Example
jQuery $.post() method is used to request data from a webpage and to display the returned result sent from requested page without page refresh.
Syntax:
URL :- The URL parameter is defined for the URL of requested page which may communicate with database to return results.
data :- The data parameter is defined to send some data along with the request.
,{ // Data Sending With Request To Server
name:vname,
email:vemail
}
callback
The callback parameter is defined for a function to be executed if the request gets succeeded. This contains two sub parameters , the first one holds the returned data from the requested page and second one holds the status of the request.
,function(response,status){ // Required Callback Function
//"response" receives - whatever written in echo of above PHP script.
alert("*----Received Data----*\n\nResponse : " + response+"\n\nStatus : " + status);
}
Note : Both "data" and "callback" parameters are optional parameters, whereas URL is mandatory for $.post() method.
Example
File name : index.php
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
<!-- Include JS File Here -->
<link href="style.css" rel="stylesheet"/>
<!-- Include JS File Here -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var vname = $("#name").val();
var vemail = $("#email").val();
if(vname=='' && vemail=='')
{
alert("Please fill out the form");
}
else if(vname=='' && vemail!==''){alert('Name field is required')}
else if(vemail=='' && vname!==''){alert('Email field is required')}
else{
$.post("jquery_post.php", //Required URL of the page on server
{ // Data Sending With Request To Server
name:vname,
email:vemail
},
function(response,status){ // Required Callback Function
alert("*----Received Data----*\n\nResponse : " + response+"\n\nStatus : " + status);//"response" receives - whatever written in echo of above PHP script.
$("#form")[0].reset();
});
}
});
});
</script>
</head>
<body>
<div id="main">
<h2>jQuery Ajax $.post() Method</h2>
<hr>
<form id="form" method="post">
<div id="namediv"><label>Name</label>
<input type="text" name="name" id="name" placeholder="Name"/><br></div>
<div id="emaildiv"><label>Email</label>
<input type="text" name="email" id="email" placeholder="Email"/></div>
</form>
<button id="btn">Send Data</button>
</div>
</body>
</html>
jquery_post.php
this page get the request, processes it and return the result to the requestd page.
File name : jquery_post.php
<?php
if($_POST["name"])
{
$name = $_POST["name"];
$email = $_POST["email"];
// Here, you can also perform some database query operations with above values.
echo "Welcome ". $name ."!"; // Success Message
}
?>
File name : index.php
File name : index.php