How to save form data using ajax
In any program using transaction of data without page loading, data is saved into database using JQuery Ajax or Simple Ajax. Ajax are widely used day to day in many light weighted web page and other software, it provide less for save data into database from website.
There are example of Jquery ajax.
This is a jquery ajax and it is compulsory to use jquery libraray.
There are another ajax called java script ajax which is created using XMLHttpRequest.
$("document").ready(function() { $("#frm_id").submit(function() //submit form when click submit button { var str=$(this).serialize(); //all input and other form fields collect data $.ajax({ url:"http://abs.com/insert.php", //url where write code for insert data into database type:"POST", data:str, success: function(result){ //result is the return data from insert.php file alert(result); }, error: function(e){ alert(JSON.stringfy(e)); //this code is give error type in you code } }); }); });
This is a jquery ajax and it is compulsory to use jquery libraray.
There are another ajax called java script ajax which is created using XMLHttpRequest.
var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //for older browsers IE } xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { alert(this.responseText); } }; xmlhttp.open("GET", "<your backend url>", true); xmlhttp.send();
1 Comments
This comment has been removed by the author.
ReplyDelete