JQuery Ajax Post Example PHP




Hi guys,

In this example, i will learn you how to pass ajax post data example with jquery php. You can simply form submit with pass ajax post data and get return all data with success.

It allows you to fetch content from the back-end without a page refresh. I will give you very simple example of ajax post request with php. you can also write server side validation using php.

You will send data one page to another page without page refresh then you can use bellow example.


<!DOCTYPE html>



<title>jquery ajax post example php</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha256-WqU1JavFxSAMcLP2WIOI+GB2zWmShMI82mTpLDcqFUg=" crossorigin="anonymous"></script>



<div class="container">

<div class="row">

<div class="col-md-6 offset-3 mt-5">

<div class="card">

<div class="card-header text-center bg-success">

<h2 class="text-white"><strong>JQuery Ajax Post Example PHP</strong></h2>


<div class="card-body">

<div class="alert alert-danger display-error" style="display: none"></div>

<form method="post" id="contactForm">

<div class="form-group">

<label>Name :</label>

<input type="text" name="name" class="form-control">


<div class="form-group">

<label>Email :</label>

<input type="email" name="email" class="form-control">


<div class="form-group">

<label>Message :</label>

<textarea class="form-control" rows="3" id="msg"></textarea>


<div class="form-group text-center">

<button class="btn btn-success" id="submit">Save</button>








<script type="text/javascript">

$(document).ready(function () {

$('#submit').click(function (e) {


var name = $('input[name="name"]').val();

var email = $('input[name="email"]').val();

var msg = $('#msg').val();


type: "POST",

url: "/process.php",

dataType: "json",

data: {name:name, email:email, msg:msg,},

success : function(data){

if (data.code == "200"){

alert("Success: " +data.msg);

} else {













$errorMSG = "";

/* NAME */

if (empty($_POST["name"])) {

$errorMSG = "<li>Name is required</li>";

} else {

$name = $_POST["name"];


/* EMAIL */

if (empty($_POST["email"])) {

$errorMSG .= "<li>Email is required</li>";

} else if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {

$errorMSG .= "<li>Invalid email format</li>";

}else {

$email = $_POST["email"];



if (empty($_POST["msg"])) {

$errorMSG .= "<li>Message is required</li>";

} else {

$msg = $_POST["msg"];



$msg = "Name: ".$name.", Email: ".$email.", Message:".$msg;

echo json_encode(['code'=>200, 'msg'=>$msg]);



echo json_encode(['code'=>404, 'msg'=>$errorMSG]);


It will help you...