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

Related Posts: You may like

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

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.