{"id":91,"date":"2022-03-10T12:22:38","date_gmt":"2022-03-10T12:22:38","guid":{"rendered":"https:\/\/pratimeshtiwari.com\/blogs\/?p=91"},"modified":"2022-07-07T16:22:43","modified_gmt":"2022-07-07T15:22:43","slug":"create-a-contact-form-with-ajax-no-reload-for-your-webpage-along-with-phpjs","status":"publish","type":"post","link":"https:\/\/pratimeshtiwari.com\/blogs\/?p=91","title":{"rendered":"Create a contact form with ajax [no reload] for your webpage along with PHP,JS with bootstrap."},"content":{"rendered":"\n<p class=\"has-medium-font-size\">Creating and Submitting forms in html is fairly easy but what makes it a bit distracting is the reload after submitting the form. Essentially this doesn&#8217;t hinder the site&#8217;s functionality but for better user experience ,AJAX can be used along with php and js to tackle this and mimics the same functionality on the same page<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Source Code : <\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">HTML :<\/h2>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\"><code>&lt;div class=\"col-lg-8 mt-5 mt-lg-0\"&gt;\n&lt;form id=\"contact-form\" name=\"contact-form\" action=\"mail.php\" method=\"POST\" onsubmit=\"return validateForm()\" &gt;\n&lt;div class=\"row\"&gt;\n&lt;div class=\"col-md-6 form-group\"&gt;\n&lt;input type=\"text\" name=\"name\" class=\"form-control\" id=\"name\" placeholder=\"Your Name\" required&gt;\n&lt;div class=\"col-md-6 form-group mt-3 mt-md-0\"&gt;\n&lt;input type=\"email\" class=\"form-control\" name=\"email\" id=\"email\" placeholder=\"Your Email\" required&gt;\n&lt;\/div&gt;\n&lt;\/div&gt;\n&lt;div class=\"form-group mt-3\"&gt;\n&lt;input type=\"text\" class=\"form-control\" name=\"subject\" id=\"subject\" placeholder=\"Subject\" required&gt;\n&lt;\/div&gt;\n&lt;div class=\"form-group mt-3\"&gt;\n&lt;textarea class=\"form-control\" name=\"message\" rows=\"5\" placeholder=\"Message\" required&gt;&lt;\/textarea&gt;\n&lt;\/div&gt;\n &lt;\/form&gt;\n&lt;div class=\"center-on-small-only\"&gt;\n&lt;a class=\"btn btn-primary\" onclick=\"validateForm()\"&gt;Send&lt;\/a&gt;\n&lt;\/div&gt; &lt;div class=\"status\" id=\"status\"&gt;&lt;\/div&gt;\n&lt;\/div&gt;\n&lt;div class=\"status\"&gt;&lt;\/div&gt;\n&lt;\/div&gt;\n&lt;\/div&gt;\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Javascript:<\/h2>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\"><code>&lt;!-- JQuery --&gt;\n&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/jquery\/3.2.1\/jquery.min.js\"&gt;&lt;\/script&gt;\n&lt;!-- Bootstrap tooltips --&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/popper.min.js\"&gt;&lt;\/script&gt;\n&lt;!-- Bootstrap core JavaScript --&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/bootstrap.js\"&gt;&lt;\/script&gt;\n&lt;!-- MDB core JavaScript --&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/mdb.js\"&gt;&lt;\/script&gt;\n\n&lt;!-- Latest compiled and minified CSS --&gt;\n&lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@3.3.7\/dist\/css\/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz\/K68vbdEjh4u\" crossorigin=\"anonymous\"&gt;\n\n&lt;!-- Optional theme --&gt;\n&lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@3.3.7\/dist\/css\/bootstrap-theme.min.css\" integrity=\"sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl\/Sp\" crossorigin=\"anonymous\"&gt;\n\n&lt;!-- Latest compiled and minified JavaScript --&gt;\n&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@3.3.7\/dist\/js\/bootstrap.min.js\" integrity=\"sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\n\n&lt;!--Custom scripts--&gt;\n&lt;script&gt;\nfunction validateForm() {\n    \/\/ var x =  document.getElementById('name').value;\n    \/\/ if (x == \"\") {\n    \/\/     document.getElementById('status').innerHTML = \"Name cannot be empty\";\n    \/\/     return false;\n    \/\/ }\n    \/\/ x =  document.getElementById('email').value;\n    \/\/ if (x == \"\") {\n    \/\/     document.getElementById('status').innerHTML = \"Email cannot be empty\";\n    \/\/     return false;\n    \/\/ } else {\n    \/\/     var re = \/^((&#91;^&lt;&gt;()\\&#91;\\]\\\\.,;:\\s@\"]+(\\.&#91;^&lt;&gt;()\\&#91;\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\&#91;&#91;0-9]{1,3}\\.&#91;0-9]{1,3}\\.&#91;0-9]{1,3}\\.&#91;0-9]{1,3}])|((&#91;a-zA-Z\\-0-9]+\\.)+&#91;a-zA-Z]{2,}))$\/;\n    \/\/     if(!re.test(x)){\n    \/\/         document.getElementById('status').innerHTML = \"Email format invalid\";\n    \/\/         return false;\n    \/\/     }\n    \/\/ }\n    \/\/ x =  document.getElementById('subject').value;\n    \/\/ if (x == \"\") {\n    \/\/     document.getElementById('status').innerHTML = \"Subject cannot be empty\";\n    \/\/     return false;\n    \/\/ }\n    \/\/ x =  document.getElementById('message').value;\n    \/\/ if (x == \"\") {\n    \/\/     document.getElementById('status').innerHTML = \"Message cannot be empty\";\n    \/\/     return false;\n    \/\/ }\n \/\/get input field values data to be sent to server\n    document.getElementById('status').innerHTML = \"Sending...\";\n    formData = {\n        'name'     : $('input&#91;name=name]').val(),\n        'email'    : $('input&#91;name=email]').val(),\n        'subject'  : $('input&#91;name=subject]').val(),\n        'message'  : $('textarea&#91;name=message]').val()\n    };\n\n$.ajax({\n    url : \"mail.php\",\n    type: \"POST\",\n    data : formData,\n    success: function(data, textStatus, jqXHR)\n    {\n\n        $('#status').text(data.message);\n        if (data.code) \/\/If mail was sent successfully, reset the form.\n        $('#contact-form').closest('form').find(\"input&#91;type=text], textarea\").val(\"\");\n    },\n    error: function (jqXHR, textStatus, errorThrown)\n    {\n        $('#status').text(jqXHR);\n    }\n});\n\n}\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PHP : <\/h2>\n\n\n\n<p class=\"has-medium-font-size\">Create a mail.php file in the same directory as the .html file and set your desired value in the $recipient section<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\"><code>&lt;?php\n$name = $_POST&#91;'name'];\n$email = $_POST&#91;'email'];\n$message = $_POST&#91;'message'];\n$subject = $_POST&#91;'subject'];\nheader('Content-Type: application\/json');\nif ($name === ''){\n  print json_encode(array('message' =&gt; 'Name cannot be empty', 'code' =&gt; 0));\n  exit();\n}\nif ($email === ''){\n  print json_encode(array('message' =&gt; 'Email cannot be empty', 'code' =&gt; 0));\n  exit();\n} else {\n  if (!filter_var($email, FILTER_VALIDATE_EMAIL)){\n  print json_encode(array('message' =&gt; 'Email format invalid.', 'code' =&gt; 0));\n  exit();\n  }\n}\nif ($subject === ''){\n  print json_encode(array('message' =&gt; 'Subject cannot be empty', 'code' =&gt; 0));\n  exit();\n}\nif ($message === ''){\n  print json_encode(array('message' =&gt; 'Message cannot be empty', 'code' =&gt; 0));\n  exit();\n}\n$content=\"From: $name \\nEmail: $email \\nMessage: $message\";\n$recipient = \"yourmail@website.com\";\n$mailheader = \"From: $email \\r\\n\";\nmail($recipient, $subject, $content, $mailheader) or die(\"Error!\");\nprint json_encode(array('message' =&gt; 'Email successfully sent!', 'code' =&gt; 1));\nexit();\n?&gt;\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating and Submitting forms in html is fairly easy but what makes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":152,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[15,17,18,16],"class_list":["post-91","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-ajax","tag-contactform","tag-form","tag-js"],"_links":{"self":[{"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91"}],"version-history":[{"count":4,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions\/180"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/media\/152"}],"wp:attachment":[{"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}