What is jQuery
1-jQuery is a fast and concise JavaScript Library created by John Resig in2006with a nice motto-
2-Write less,do more.
3-jQuery simplifies HTML document traversing,event handling,animating,and Ajax interactions for rapid web development.
Following feature of jQuery:
1-DOM manipulation−The jQuery made it easy to select DOM elements,traverse them and modifying their content by using cross-browser open source selector engine called Sizzle.
2-EventHandling:The jQuery offers an elegant way to capture a wide variety of events,such as click,over event,
3-Ajax Support:
jQuery support ajax technology,
4-Lightweight:he jQuery is very lightweight library-about19KB in size (Minified and gzipped ).
5-Animations−The jQuery comes with plenty of built-in animation effects.
6-Cross Browser Support−The jQuery has cross-browser support,and works well in IE and all.
AJAXstands forAsynchronous JavaScript and XML.
AJAX is a new technique for creating better,faster,and more interactive web applications with the help of XML,HTML,CSS,and Java Script
Before you start ajax you will be much and more knowledge about javascript,its important for us.ajax is not difficult it can be easily implementation.
Following advantage of ajax call:
1-Speed,
2-Interaction,
3-XMLHttpRequest,
4-Asynchronous call,
5-Form Validation,
6-Bandwidth Usage,
AJAX Disadvantages
1-Open Source:View source is allowed and anyone can view the code source written for AJAX,
2-It can increase design and development time,
3-Security is less in AJAX application as all files are downloaded at client side.
Synchronous (async:false )–
1-Script stops and waits for the server to send back a reply before continuing.There are some situations where Synchronous Ajax is mandatory.
In standard Web applications,the interaction between the customer and the server is synchronous.This means that one has to happen after the other.If a customer clicks a link,the request is sent to the server,which then sends the results back.
$.ajax({
url: "file.php",
type: "POST",
async:false,
success:function(data) {
//.....
}
});
Demo link :
Code Link :
following example is very simple in ajax call only difference between synchronus call and asynchronus call async:false and async:true respectively.
Asynchronous (async:true )–
1-Where the script allows the page to continue to be processed and will handle the reply if and when it arrives.
2-If anything goes wrong in the request and/or transfer of the file,your program still has the ability to recognize the problem and recover from it.
3-Processing asynchronously avoids the delay while the retrieval from the server is taking place because your visitor can continue to interact with the web page and the requested information will be processed with the response updating the page as and when it arrives.
Code Link :
$.ajax({
url: "file.php",
type: "POST",
async:true,
success:function(data) {
//.....
}
});
Processing asynchronously avoids the delay while the retrieval from the server is taking place because your visitor can continue to interact with the web page and the requested information will be processed with the response updating the page as and when it arrives
Events
This is the full list of Ajax events,and in the order in which they are triggered.The ajaxStart and ajaxStop events are events that relate to all Ajax requests together.
ajaxStart (Global Event)
This event is triggered if an Ajax request is started and no other Ajax requests are currently running.
beforeSend (Local Event)
This event,which is triggered before an Ajax request is started,allows you to modify the XMLHttpRequest object (setting additional headers,if need be.)
ajaxSend (Global Event)
This global event is also triggered before the request is run.
success (Local Event)
This event is only called if the request was successful (no errors from the server,no errors with the data).
ajaxSuccess (Global Event)
This event is also only called if the request was successful.
error (Local Event)
This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request).
ajaxError (Global Event)
This global event behaves the same as the local error event.
complete (Local Event)
This event is called regardless of if the request was successful,or not.You will always receive a complete callback,even for synchronous requests.
ajaxComplete (Global Event)
This event behaves the same as the complete event and will be triggered every time an Ajax request finishes.
ajaxStop (Global Event)
This global event is triggered if there are no more Ajax requests being processed.
following file create :
1-index.php,
2-action.php,
3-header.php,
4-footer.php,
1-index.php file include header.php file and body tag contain html input field, in header section jquery as well as bootsrap file link, simply ajax function call on form submitonSubmit="return ajaxSubmit(this);” follwing ajax call on from submit.
<script>
function ajaxSubmit(){
var fname = $.trim($('#fname').val());
var lname = $.trim($('#lname').val());
$.ajax({
type: "POST",
url: "action.php",
async: true,
data: {fname:fname,lname:lname},
success: function(html) {
var result=$.parseJSON(html);
if(result.status==2) {
alert('First name is empty.');
} else if(result.status==3) {
alert('Last name is empty.');
} else if(result.status==1) {
$("label[for='fnamelb']").html(result.fname);
$("label[for='lnamelb']").html(result.lname);
} else {
alert('Something goes wrong.');
}
}
});
return false;
}
</script>
only async value true or false
index.php full code here :
<?php include('header.php'); ?>
<body>
<br><br><br>
<div class="cleafix"></div>
<div class="container">
<div class="alert alert-info">
<strong>Hi..</strong> jQuery Asynchronous Ajax Call.
</div>
</div>
<div class="container text-center card">
<div class="col-md-6">
<form action="" method="post" id="signupFrom" onSubmit="return ajaxSubmit(this);" name="signupFrom" accept-charset="utf-8">
<label for="name">First Name</label>
<input type="text" name="fname" id="fname" class="form-control" placeholder="Enter First Name..." />
<div class="clearfix"></div>
<label for="name">Last Name</label>
<input type="text" name="lname" id="lname" class="form-control" placeholder="Enter Last Name..." />
<div class="clearfix"></div>
</br>
<button type="submit" name="submit" id="submit" class="btn btn-primary">Singup</button>
</form>
</div>
<div class="clearfix"></div>
<div class="col-md-6">
First Name :<label for="fnamelb"></label>
<div class="clearfix"></div>
Last Name : <label for="lnamelb"></label>
</div>
</div>
</br>
<?php include('footer.php'); ?>
<script>
function ajaxSubmit()
{
var fname = $.trim($('#fname').val());
var lname = $.trim($('#lname').val());
$.ajax({
type: "POST",
url: "action.php",
async: true,
data: {fname:fname,lname:lname},
success: function(html) {
var result=$.parseJSON(html);
if(result.status==2) {
alert('First name is empty.');
} else if(result.status==3) {
alert('Last name is empty.');
} else if(result.status==1) {
$("label[for='fnamelb']").html(result.fname);
$("label[for='lnamelb']").html(result.lname);
} else {
alert('Something goes wrong.');
}
}
});
return false;
}
</script>
2-action.php :
action .php file check form submit value is empty or not if the value is empty alert will be generated and if firstname and last name is filled then json_encode() function encode post value in array with response and post value send to index.php page
Code :
<?php
/*
*Post value::-
*/
$fname=$_POST['fname'];
$lname=$_POST['lname'];
if(empty($fname)) {
$post_array=array('status'=>2);
echo json_encode($post_array);
die();
} else if(empty($lname)) {
$post_array=array('status'=>3);
echo json_encode($post_array);
die();
} else if(!empty($fname) && !empty($lname)) {
$post_array=array('status'=>1,'fname'=>$fname,'lname'=>$lname);
echo json_encode($post_array);
die();
}else
{
$post_array=array('status'=>0);
echo json_encode($post_array);
die();
}
?>
3-header.php :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Asynchronous Ajax Call</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<!-- start:header -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand" href="#">Free Teachnology</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home <span class="sr-only"></span></a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Contact us</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</br></br></br>
<!-- end:header -->
4-footer.php :
<script>
// $(".errorMsg").alert('close');
$(".errorMsg").slideUp(2000);
</script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<!-- Start::footer -->
<footer>
<div class="footer footer-bottom">
<div class="container">
<div class="text-muted ">
<a href="https://freeteachnology.blogspot.in/" >Free Teachnology</a>
</div>
<p class="pull-left"> Copyright © <?php echo date('Y');?>. Design and Develop by - Disuza Jen. </p>
<div class="pull-right">
<ul class="nav nav-pills payments">
<li><i class="fa fa-facebook"></i></li>
<li><i class="fa fa-google"></i></li>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-link"></i></li>
</ul>
</div>
</div>
</div>
<!--/.footer-bottom-->
</footer>
<!-- End::footer -->
<style type="text/css">
.footer-bottom {
margin-top:220px;
background: #E3E3E3;
border-top: 1px solid #DDDDDD;
padding-top: 10px;
padding-bottom: 10px;
}
.footer-bottom p.pull-left {
padding-top: 6px;
}
</style>
</body>
</html>
</html>

Comments