{"id":603,"date":"2014-08-08T13:50:04","date_gmt":"2014-08-08T13:50:04","guid":{"rendered":"http:\/\/a1webdesignteam.com\/blog\/?p=603"},"modified":"2014-08-08T13:50:04","modified_gmt":"2014-08-08T13:50:04","slug":"submit-form-using-ajax-without-refreshing-page","status":"publish","type":"post","link":"https:\/\/a1webdesignteam.com\/blog\/submit-form-using-ajax-without-refreshing-page\/","title":{"rendered":"Submit form using ajax without refreshing page"},"content":{"rendered":"<p>Today, I\u2019ll teach you, how to submit form using ajax, without refreshing page. Its very simple and easy way.<br \/>\nYou just need to download latest jQuery. I\u2019ve give you a very simple example so that everyone can understand it easily.<\/p>\n<p>we just need to add jquery file into your page. We can submit the form without refreshing page.<br \/>\nTo understand it completely just follow the steps.<br \/>\n<strong><span style=\"font-size: large;\">Step-1:<\/span><\/strong><br \/>\nCreate a file <strong>form.html <\/strong>and insert some input fields. For this tutorials, Insert three text fields (First Name, Last Name, Email) and set their ids and names to <strong>fname<\/strong>, <strong>lname<\/strong>, &amp; <strong>email<\/strong>. and a Submit button.<br \/>\nNow Place a div at the bottom and set its id to <strong>status<\/strong>.<\/p>\n<p><strong>Code:<\/strong><br \/>\n<code><br \/>\n&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;<br \/>\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt;<br \/>\n&lt;title&gt;Submit Form Using ajax by Sharp Coders&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\"&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;First Name&lt;\/td&gt;<br \/>\n&lt;td&gt;&lt;input type=\"text\" name=\"fname\" id=\"fname\" \/&gt;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;Last Name&lt;\/td&gt;<br \/>\n&lt;td&gt;&lt;input type=\"text\" name=\"lname\" id=\"lname\" \/&gt;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;Email&lt;\/td&gt;<br \/>\n&lt;td&gt;&lt;input type=\"text\" name=\"email\" id=\"email\" \/&gt;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;&amp;nbsp;&lt;\/td&gt;<br \/>\n&lt;td&gt;&lt;button type=\"button\" name=\"btnSubmit\" id=\"btnSubmit\"&gt;Submit&lt;\/button&gt;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;\/table&gt;<br \/>\n&lt;div id=\"status\"&gt;<\/code><\/p>\n<p>&lt;\/div&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<div><span style=\"font-size: large;\"><strong>Step-2<\/strong><\/span><\/div>\n<div>Now <a href=\"http:\/\/docs.jquery.com\/Downloading_jQuery\" target=\"_blank\">download jquery file<\/a> \u00a0and place this file in the project folder, and add this file to our page.<\/div>\n<div>to do this place this code in the head section of the <strong>form.html<\/strong> page.<\/div>\n<div><strong>Code:<\/strong><\/div>\n<p><code>&lt;script src=\"jquery.min.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;<\/code><\/p>\n<div>Now, we can use jquery in our page.<\/div>\n<div>Now add the following code in the head section<\/div>\n<div><strong>code:<\/strong><\/div>\n<div><code><br \/>\n&lt;script type=\"text\/javascript\"&gt;<br \/>\n$(document).ready(function(){<br \/>\n$('#btnSubmit').click(function(){<br \/>\n$.ajax({<br \/>\ntype: \"POST\",<br \/>\nurl: 'form2.php',<br \/>\ndata: \"fname=\" + $('#fname').val() + \"&amp;lname=\" + $('#lname').val() + \"&amp;email=\" + $('#email').val(),<br \/>\nsuccess: function(data){<br \/>\n$('#status').html(data);<br \/>\n}<br \/>\n});<br \/>\n});<br \/>\n});<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><br \/>\nThis code will be used to submit form through ajax.<strong>Final Code of form.html page\u00a0<\/strong><br \/>\n<code><br \/>\n&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;<br \/>\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt;<br \/>\n&lt;title&gt;Submit Form Using jQuery\/ajax by Sharp Coders&lt;\/title&gt;<br \/>\n&lt;script src=\"jquery.min.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;<br \/>\n&lt;script type=\"text\/javascript\"&gt;<br \/>\n$(document).ready(function(){<br \/>\n$('#btnSubmit').click(function(){<br \/>\n$.ajax({<br \/>\ntype: \"POST\",<br \/>\nurl: 'form2.php',<br \/>\ndata: \"fname=\" + $('#fname').val() + \"&amp;lname=\" + $('#lname').val() + \"&amp;email=\" + $('#email').val(),<br \/>\nsuccess: function(data){<br \/>\n$('#status').html(data);<br \/>\n}<br \/>\n});<br \/>\n});<br \/>\n});<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\"&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;First Name&lt;\/td&gt;<br \/>\n&lt;td&gt;&lt;input type=\"text\" name=\"fname\" id=\"fname\" \/&gt;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;Last Name&lt;\/td&gt;<br \/>\n&lt;td&gt;&lt;input type=\"text\" name=\"lname\" id=\"lname\" \/&gt;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;Email&lt;\/td&gt;<br \/>\n&lt;td&gt;&lt;input type=\"text\" name=\"email\" id=\"email\" \/&gt;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td&gt;&amp;nbsp;&lt;\/td&gt;<br \/>\n&lt;td&gt;&lt;button type=\"button\" name=\"btnSubmit\" id=\"btnSubmit\"&gt;Submit&lt;\/button&gt;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;\/table&gt;<br \/>\n&lt;br \/&gt;<br \/>\n&lt;a href=\"http:\/\/www.sharp-coders.com\"&gt;http:\/\/sharp-coders.blogspot.com&lt;\/a&gt;<br \/>\n&lt;br \/&gt;<br \/>\n&lt;div id=\"status\"&gt;<\/code>&nbsp;<\/p>\n<p>&lt;\/div&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<div style=\"font-weight: bold;\"><\/div>\n<\/div>\n<div>\n<div><span style=\"font-size: large;\"><strong>Step-3<\/strong><\/span><\/div>\n<div>Create an Empty php file with the name\u00a0<strong>form2.php\u00a0<\/strong>and place following code in the file.<\/div>\n<div><code><br \/>\n&lt;?php<br \/>\n$fname = $_POST['fname'];<br \/>\n$lname = $_POST['lname'];<br \/>\n$email = $_POST['email'];<br \/>\necho \"You Entered &lt;br \/&gt;\";<br \/>\necho \"&lt;b&gt;First Name:&lt;\/b&gt; \". $fname . \"&lt;br \/&gt;\";<br \/>\necho \"&lt;b&gt;Last Name:&lt;\/b&gt; \". $lname . \"&lt;br \/&gt;\";<br \/>\necho \"&lt;b&gt;Email:&lt;\/b&gt; \". $email . \"&lt;br \/&gt;\";<br \/>\n?&gt;<br \/>\n<\/code><\/div>\n<div>When we\u2019ll submit the form, the form values will be posted to this (form2.php) page. You can perform different operation on the receiving data.<\/div>\n<div>We\u2019ve just printed the receiving data.<\/div>\n<\/div>\n<div style=\"text-align: center;\"><strong><a href=\"http:\/\/www.4shared.com\/rar\/oHstQvPe\/submit_form_with_ajax_sharp-co.html\" target=\"_blank\"><span style=\"font-size: large;\">Download Complete Code<\/span><\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today, I\u2019ll teach you, how to submit form using ajax, without refreshing page. Its very simple and easy way. You just need to download latest jQuery. I\u2019ve give you a very simple example so that everyone can understand it easily. we just need to add jquery file into your page. We can submit the form [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":604,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/603"}],"collection":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/comments?post=603"}],"version-history":[{"count":0,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media\/604"}],"wp:attachment":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}