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.

Mukesh Jakhar - PHP Web Developer
Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.

I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

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.