AJAX = Asynchronous JavaScript And XML
AJAX is a client-side script which is used to communicate to the server. It is used to get or submit the data or update the part of the webpage without refresh the page.When we want to submit a form, first we get the form data using serialize method following code:-
var datastring = $("#create-from").serialize();
If want to send custom data then,
var datastring = {name:"ABC",email:"abc@gmail.com"}
Now we will submit the form data using ajax
$.ajax({
var datastring = {name:"ABC",email:"abc@gmail.com"}
Now we will submit the form data using ajax
$.ajax({
type:'POST',
url:'asySubmit.php',
data:datastring,
beforeSend: function(){
$('.loader').show();
},
success:function(response) {
console.log(response);
},
error:function(response) {
alert(response);
}
});
If the form contains file input then use following code:-
$.ajax({
type:'POST',
url:'asySubmit.php',
data:datastring,
processData: false,
contentType: false,
beforeSend: function(){
$(".loader").show();
},
success:function(data) {
console.log(data);
},
error:function(data) {
alert('Error occured while inserting record');
}
});
Following is shortform of ajax
$.post( "submit.php", { name:"Abc", id:1 },function( data ) {
Following is shortform of ajax
$.post( "submit.php", { name:"Abc", id:1 },function( data ) {
//data
});
Comments
Post a Comment