How to Post Data Using AJAX and PHP Tutorial


In this tutorial we learn how to post data using Ajax and PHP in a Simple Example.


Ajax (Asynchronous JavaScript and XML) is a Technic that allow the web application to be updated asynchronously with data without load server side.

In this tutorial, we will submit a form by using Ajax. After Click the submit button, Ajax will post the data asynchronously. We can post any type of data like simple text field, Images, Files, Dropdown list etc. For that we will create a simple form and send the data by Ajax. PHP(Any backend script) is used to get post data in the server side.

Create a HTML Form

We will create a simple form in html. We need to define form Id. In the below form, form id is ‘myform’. We will use form id to get all input data by Ajax.

<!DOCTYPE html>
    <title>Form Validation</title>
    <!-- Latest compiled and minified BootStarp CSS -->
    <link rel="stylesheet" href="">
    <!-- jQuery library -->
    <script src=""></script>
    <div id="page-wrapper" >
      <div class="container" >
          Send Post Data to AJAX
        <form name="myForm" class="form-horizontal" id ="myform" action="#"  
                 method="POST" enctype="multipart/form-datam">
          <div class="panel panel-info">
            <div class="panel-heading">Form</div>
            <div class="panel-body">
              <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <label for="name" class="col-md-4">Name *</label>
                      <div class="col-md-6">
                        <input type="text" class="form-control" id="name" name="name" 
                                placeholder="Enter Name" required/>
                    <div class="form-group" >
                      <label for="Mobile" class="col-md-4">Mobile *</label>
                      <div class="col-md-6">
                        <input type="text" class="form-control"  id="mobile" name="phone"
                                 placeholder="Enter Mobile Number" required/>
                    <div class="form-group  ">
                      <label for="email" class="col-md-4">Email </label>
                      <div class="col-md-6">
                        <input type="text" class="form-control" name="email" 
                             placeholder="Enter  Email" >
                     <div class="form-group  ">
                      <label for="image" class="col-md-4">Select Image </label>
                      <div class="col-md-6">
                        <input type="file" class="form-control" name="image" >
          <div class="col-md-4 col-md-offset-4" style="margin-bottom: 50px;">
              <input type="submit" class="btn btn-primary" value="Submit" />


Create Js Function

Now, We will create a method in the js file. This method is used to send POST data by using AJAX.

function submitForm() {
  var form_data = new FormData(document.getElementById("myform"));
  form_data.append("label", "WEBUPLOAD");
      url: "YOUR URL HERE",
      type: "POST",
      data: form_data,
      processData: false,  // tell jQuery not to process the data
      contentType: false   // tell jQuery not to set contentType
  }).done(function( data ) {
    //Perform ANy action after successfuly post data
  return false;     

Call JS function on Click submit button

We will add onsubmit=”return submitForm();” in the <form> tag. When we add onsubmit in the form tag, then it will be call submitform() method after click on the submit form to send post data to server.

<form name="myForm" class="form-horizontal" id ="myform" action="#"  
        onsubmit="return submitForm();"method="POST" enctype="multipart/form-datam">

Get POST Request in the backend

We can use any back-end script to get post request but here we will use PHP.

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$image = $_FILES['image'];

  • zacharia nyang’oro

    thanks much

  • sachin jaiswal

    awesome tutorial thanks a lot