Send POST Request Using Ajax In PHP | CampCodes ...

Send POST Request Using Ajax in PHP

July 14, 2020
Send POST Request Using Ajax in PHP

Tutorial: Send POST Request Using Ajax in PHP

In this tutorial, we will create a Send POST Request using Ajax in PHP. This code will send a PHP post request when the user submits the form inputs. The system uses jQuery ajax to send a request and immediately retrieve the MySQLi table row server without a refreshing web page and display it as a readable data in the HTML table. This is a user-friendly kind of program that feels free to the user in your application.

We will be using jQuery, a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can retrieve DOM and manipulate each property of an element based on different criteria such as id, class, etc.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server

And this is the link for the jquery that I used in this tutorial

Lastly, this is the link for the bootstrap that I used for the layout design

Creating Database

Open your database web server then create a database name in it db_send; after that, click Import, then locate the database file inside the folder of the application then click ok.

send post request using ajax in php

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

    $conn = mysqli_connect("localhost", "root", "", "db_send") or die(mysqli_error());
        die("Error: Failed to connect to database");

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="">Sourcecodester</a>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Send POST Request Using Ajax</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <form method="POST">			
                <div class="form-group">
                    <input type="text" id="firstname" class="form-control"/>
                <div class="form-group">
                    <input type="text" id="lastname" class="form-control"/>
                <div class="form-group">
                    <input type="text" id="address" class="form-control"/>
                <center><button type="button" id="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button></center>
        <div class="col-md-8">
            <table class="table table-bordered">
                <thead class="alert-info">
                <tbody id="data" style="background-color:#fff;"></tbody>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/script.js"></script>

Creating the PHP Query

This code contains the save function of the application. This code will store the form data to MySQLi database. To make this just copy and write these block of codes below inside the text editor, then save it as save.php.

    require_once 'conn.php';
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $address = $_POST['address'];
    mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
    echo "Data Sent!";

Creating the Main Function

This code contains the main function of the application. This code will send a post requiest and display MySQLi data in table when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below

    require 'conn.php';
        $query = mysqli_query($conn, "SELECT * FROM `member` ORDER BY `lastname` ASC") or die(mysqli_error());
        while($fetch = mysqli_fetch_array($query)){

Note: To make the script works make sure you save this file inside the js directory.

    $('#save').on('click', function(){
        var firstname = $('#firstname').val();
        var lastname = $('#lastname').val();
        var address = $('#address').val();
        if($('#firstname').val() == "" || $('#lastname').val() == "" || $('address').val()){
            alert("Please complete the required field");
                type: 'POST',
                url: 'save.php',
                data: {
                    firstname: firstname,
                    lastname: lastname,
                    address: address
                success: function(data){
    function displayData(){
            type: 'POST',
            url: 'data.php',
            data: {res: 1},
            success: function(data){

There you have it we successfully created Send POST Request using Ajax in PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Download Here

Leave a Reply

Your email address will not be published. Required fields are marked *

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.