Submit form using ajax in php example

Today we will tell you how we can submit form data without refreshing the page using Ajax and php, This is a very simple tutorial for submitting HTML form by jquery ajax and php without refreshing the page.

Create HTML form

First we create a HTML form inside which we take four (4) common fields name, email, phone number, and message.

<form method="post" name="contactForm" id="contactForm">
 <div>
 <input type="text" name="cname" id="cname" placeholder="Name*" required="" />
 </div>
 <div>
 <input type="text" name="cemail" id="cemail" placeholder="Email*" required="" />
 </div>
 <div>
 <input type="text" name="cphone" id="cphone" placeholder="Phone*" required="" />
 </div>
 <div>
 <input type="button" onclick="return formValidation();" value="SUBMIT" />
 </div>
 <div>
 <textarea name="cmessage" id="cmessage" placeholder="Message" rows="4" required=""></textarea>
 </div>
</form>

Jquery Validation on HTML form

Our html form is ready. Now we look at html form validation using jquery and after validating we submit form using ajax on “ajaxResponce.php” page.

<script>
function formValidation()
{
 var cmessage = $('#cmessage').val();
 var cname = $('#cname').val();
 var cemail = $('#cemail').val();
 var cphone = $('#cphone').val(); 
 var emailRegex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
 var mobileRegex = /^[0-9]*$/;
 var error = 0;
 if(cname=="")
 {
 error = 1;
 document.contactForm.cname.focus();
 alert("Please enter your name!");
 return false;
 }
 else if(cemail=="")
 {
 error = 1;
 document.contactForm.cemail.focus();
 alert("Please enter your email!");
 return false;
 }
 else if(!emailRegex.test(cemail)){
 error = 1;
 document.contactForm.cemail.focus();
 alert("Please enter valid email!");
 return false;
 }
 else if(cphone=="")
 {
 error = 1;
 document.contactForm.cphone.focus();
 alert("Please enter your phone number!");
 return false;
 }
 else if(cmessage=="")
 {
 error = 1;
 document.contactForm.cmessage.focus();
 alert("Please enter your message!");
 return false;
 }
 if(error==0)
 {
 var request = jQuery.ajax({
 url: "ajaxResponce.php",
 type: "POST",
 data: {contactForm : '1', cname : cname, cemail : cemail, cphone : cphone, cmessage : cmessage},
 dataType: "html"
 });
 request.done(function(msg)
 {
 if(msg==1)
 {
 alert("Form has been submitted! Thanks.");
 $("#contactForm")[0].reset();
 }
 });
}
</script>

So our HTMl for and jquery validation with ajax is done, now we have to create “ajaxResponce.php” file and inside php file copy below code and you can edit according to your requirment.

<?php
if(isset($_POST['contactForm'] && $_POST['contactForm']==1))
{
 $cname = $_POST['cname'];
 $cemail = $_POST['cemail'];
 $cphone = $_POST['cphone'];
 $cmessage = $_POST['cmessage'];
 
 // preform your operation here.
 echo 1;
}
?>

NOTE: Please make sure while using this code, you have to include jquery library.


Tutorial Name :
Submit form using ajax in php example
Publish Date : 28-3-2018.
Keywords : submit form using ajax in php, submit form data using ajax in php, submit form data using ajax php, jquery ajax form submit php mysql, jquery ajax form submit example php, submit form using ajax without page refresh, submit form using ajax javascript, form submission using ajax php and javascript, send mail in php using ajax form, submit form using ajax jquery.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts: You may like

How to login with LinkedIn oAuth2 in PHP

Extract Total Number of Image And Missing Alt Attribute using php

How to extract Heading tags using PHP from string

How to remove whitespace from string using php

Remove All Special Characters From String PHP

Redirecting HTTP to HTTPS with PHP

Convert Base64 string to an image file

Fullscreen Background Video Using HTML5 And CSS

How to get form filed names with values php

how to get current week start date and end date in php

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.