Ajax POST request with jQuery and PHP

Admin   Jquery   617  2021-03-12 23:20:04

Today we will explain to you how to make ajax POST request with JQuery and PHP. In this article we’ll simply create form in HTML and submit form data via ajax POST request. Later on you can insert it into the database or use it in your further logic.



<!DOCTYPE html>



<title>Ajax POST request with JQuery and PHP</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<style type="text/css">

body {

font-family: calibri;


.box {

margin-bottom: 10px;


.box label {

display: inline-block;

width: 80px;

text-align: right;

margin-right: 10px;


.box input, .box textarea {

border-radius: 3px;

border: 1px solid #ddd;

padding: 5px 10px;


.btn-submit {

margin-left: 90px;





<h2>Ajax POST request with JQuery and PHP - <a href="https://www.cluemediator.com" target="_blank" rel="nofollow" target="_blank">Clue Mediator</a></h2>


<div class="box">

<label>First Name:</label><input type="text" name="firstName" id="firstName" />


<div class="box">

<label>Last Name:</label><input type="text" name="lastName" id="lastName" />


<div class="box">

<label>Email:</label><input type="email" name="email" id="email" />


<div class="box">

<label>Message:</label><textarea type="text" name="message" id="message"></textarea>


<input id="submit" type="button" class="btn-submit" value="Submit" />



$(document).ready(function() {

$("#submit").click(function() {

var firstName = $("#firstName").val();

var lastName = $("#lastName").val();

var email = $("#email").val();

var message = $("#message").val();

if(firstName==''||lastName==''||email==''||message=='') {

alert("Please fill all fields.");

return false;



type: "POST",

url: "store.php",

data: {

firstName: firstName,

lastName: lastName,

email: email,

message: message


cache: false,

success: function(data) {



error: function(xhr, status, error) {













* Write your logic to manage the data

* like storing data in database


// POST Data

$data['name'] = $_POST['firstName'] . " " . $_POST['lastName'];

$data['email'] = $_POST['email'];

$data['message'] = $_POST['message'];

echo json_encode($data);



It will help you...