How do post data from a form to a PHP file using Ajax

Create a PHP file for creating the form

<form class="container-fluid bill-rma-pdt-detail-container" id="rmaform">
   <input type=text name="firstname">
</form

write js script for Ajax request

<script>
$("#rmaform").submit(function(e) {  
        e.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            type: "post",
            beforeSend: function(){
               $('.page-loader').show();
            },
            url: rma-submit.php// server-side script URL
            showLoader: true,
            data: formData,
            dataType: 'json',
            asyc: false,
            success:function(response) {
            }
});

Create rma-submit.php file

$post_data will receive all the values sent from the form

<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');

if(!empty($_POST)){
$post_data = $_POST;
}

Thank you.

Leave a comment

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