PHP Ajax Form Validation without page refresh

PHP Ajax Form Validation without page refresh

PHP Ajax Form Validation without page refresh

In this Post We Will Explain About is PHP Ajax Form Validation without page refresh With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to submit form using ajax without page refresh Example

In this post we will show you Best way to implement ajax contact form with validation, hear for php form validation example code download with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Simple PHP Ajax Form Validation Example from scratch

Here, We will make PHP simple contact us form submit using html with php ajax source code bootstrap framework. When We simple click on “Send Comments” button it will simple call php file fire ajax post jquery send request and check check some validation and show display it as now on simple bellow source code preview. We will make two simple file following files for index.php, and do_submit.php and include scripts this example:

  PHP AJAX Live Search Box Autocomplete Using MySQL Database

index.php

<!DOCTYPE html>
<html>
<head>
	<title>Pakainfo.com Php Ajax Form simple Validation Example</title>
	<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="script.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>

<div class="container">
  <h1>Step By Step Php Ajax Form simple Validation Example</h1>
  <form role="form" id="contactForm" class="contact-form" data-toggle="validator" class="shake">
    <div class="alert alert-danger display-live-error" style="display: none">
    </div>
    <div class="form-group">
      <div class="controls">
        <input type="text" id="name" class="form-control" placeholder="Enter your Name">
      </div>
    </div>
    <div class="form-group">
      <div class="controls">
        <input type="email" class="email form-control" id="email" placeholder="Enter your Email" >
      </div>
    </div>
    <div class="form-group">
      <div class="controls">
        <input type="text" id="mail_sub" class="form-control" placeholder="Enter your Subject" >
      </div>
    </div>
    <div class="form-group">
      <div class="controls">
        <textarea id="comments" rows="7" placeholder="Enter your Massage" class="form-control"></textarea>
      </div>  
    </div>
    <button type="submit" id="submit" class="btn btn-success"><i class="fa fa-check"></i> Send Comments</button>
  </form>
</div>
</body>
</html>

script.js

<script type="text/javascript">
  $(document).ready(function() {
	//form submit live
      $('#submit').click(function(e){
        e.preventDefault();

        var name = $("#name").val();
        var email = $("#email").val();
        var mail_sub = $("#mail_sub").val();
        var comments = $("#comments").val();

        $.ajax({
            type: "POST",
            url: "/do_submit.php",
            dataType: "json",
            data: {name:name, email:email, mail_sub:mail_sub, comments:comments},
            success : function(data){
                if (data.code == "200"){
                    alert("Success: " +data.msg);
                } else {
                    $(".display-live-error").html("<ul>"+data.msg+"</ul>");
                    $(".display-live-error").css("display","block");
                }
            }
        });

      });
  });
</script>

do_submit.php

<?php

$live_comments_error = "";

/* Devloped by Pakainfo.com NAME */
if (empty($_POST["name"])) {
    $live_comments_error = "<li>Your Name is required</<li>";
} else {
    $name = $_POST["name"];
}

/* Devloped by Pakainfo.com EMAIL */
if (empty($_POST["email"])) {
    $live_comments_error .= "<li>Your Email is required</li>";
} else if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
    $live_comments_error .= "<li>Invalid email format</li>";
}else {
    $email = $_POST["email"];
}

/*Devloped by Pakainfo.com MSG SUBJECT */
if (empty($_POST["mail_sub"])) {
    $live_comments_error .= "<li>Your Subject is required</li>";
} else {
    $mail_sub = $_POST["mail_sub"];
}

/*Devloped by Pakainfo.com MESSAGE */
if (empty($_POST["comments"])) {
    $live_comments_error .= "<li>Your Comments is required</li>";
} else {
    $comments = $_POST["comments"];
}

if(empty($live_comments_error)){
	$msg = "Your Name: ".$name.", Your Email: ".$email.", Your Subject: ".$mail_sub.", Your Comments:".$comments;
	echo json_encode(['code'=>200, 'msg'=>$msg]);
	exit;
}
//return form message 
echo json_encode(['code'=>404, 'msg'=>$live_comments_error]);

?>

You are Most welcome in my youtube Channel Please shubscibe my channel. and give me FeedBack.
More Details……
Angularjs Example

  Generate Secure Random Strings using Laravel

Example

I hope you have Got What is Simple PHP Ajax Form Validation Example from scratch And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.