Insert Data using Ajax Jquery and PHP | YourSite

Insert Data using Ajax Jquery and PHP

Syntax: index.html



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    
</head>
<body>


<div id="container" class="col-xs-6 col-xs-offset-3"> 
    
<div class="row">  
    <h2> Table Operations </h2>
       <form method="post" id="add-subject-form" class="col-x-6" action="add_subject.php"> 
        <div class="form-group"> 
            <label for="subject-name">Add a Subject</label> 
            <input type="text" name="subject_name" class="form-control" required> 
        </div> 
          <div class="form-group">  
            <input type="submit" class="btn btn-primary" value="add subject"> 
         </div>  
       </form> 

 <div class="col-xs-6">
    <div id="subject-result"> </div>
 </div> 

    </div>

 </div>  
 

</body>
</html>

 <script>
     
   $(document).ready(function(){ 
         
   $("#add-subject-form").submit(function(evt){ 
    
       evt.preventDefault();
       
       var postData = $(this).serialize();  
       var url = $(this).attr('action'); 
       // alert(postData);
       
       $.post(url, postData, function(php_table_data){ 
     
          $("#subject-result").html(php_table_data); 
         // $("#add-car-form")[0].reset(); 
     
     });  
   
   });
});
 
 </script>
  


Syntax: add_subject.php



<?php

$connection = mysqli_connect('localhost', 'root', '','atnyla');


if(isset($_POST['subject_name'])){

	echo 'Data recieved <br>';
	echo $_POST['subject_name'];
}

?>


Without Page load add a form value using Ajax

Syntax: Index.html



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    
</head>
<body>


<div id="container" class="col-xs-6 col-xs-offset-3"> 
    
<div class="row">  
    <h2> Table Operations </h2>
       <form method="post" id="add-subject-form" class="col-x-6" action="add_subject.php"> 
        <div class="form-group"> 
            <label for="subject-name">Add a Subject</label> 
            <input type="text" name="subject_name" class="form-control" required> 
        </div> 
          <div class="form-group">  
            <input type="submit" class="btn btn-primary" value="add subject"> 
         </div>  
       </form> 

 <div class="col-xs-6">
    <div id="subject-result"> </div>
 </div> 

    </div>

 </div>  
 

</body>
</html>

 <script>
     
   $(document).ready(function(){ 
         
   $("#add-subject-form").submit(function(evt){ 
    
       evt.preventDefault();
       
       var postData = $(this).serialize();  
       var url = $(this).attr('action');  
       $.post(url, postData, function(php_table_data){ 
     
            $("#subject-result").html(php_table_data);  
     });  
   
   });
});
 
 </script>
  


Syntax: add_subject.html



<?php
ob_start();
$connection = mysqli_connect('localhost', 'root', '','atnyla');


if(isset($_POST['subject_name'])) {
	
$subject_name = $_POST['subject_name'];
$query = "INSERT INTO subjects(Subject_Name) VALUES('$subject_name') ";
$update_subject_name = mysqli_query($connection, $query);
    
if(!$update_subject_name) {

die("QUERY FAILED");

}   
 
exit;


}

?>


Add a Subject into table and See it instantly Using Ajax in same page without page loading

Syntax: Index.html



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    
</head>
<body>


<div id="container" class="col-xs-6 col-xs-offset-3"> 
    
<div class="row">  
    <h2> Table Operations </h2>
       <form method="post" id="add-subject-form" class="col-x-6" action="add_subject.php"> 
        <div class="form-group"> 
            <label for="subject-name">Add a Subject</label> 
            <input type="text" name="subject_name" class="form-control" required> 
        </div> 
          <div class="form-group">  
            <input type="submit" class="btn btn-primary" value="add subject"> 
         </div>  
       </form> 

 <div class="col-xs-6">
    <div id="subject-result"> </div>
 </div> 


  <div class="row"> 
        <div class="col-xs-6"> 
           <table class="table">
               <thead>
                   <tr> 
                       <th>Id</th>
                       <th>Name</th>
                   </tr>
               </thead>
                 <tbody id="show-subjects"></tbody>
                   
           </table>
           
        </div>



    </div>

 </div>  
 

</body>
</html>

 <script>
     
   $(document).ready(function(){ 


     /**********Update Subject Table Display with time ****************/ 
         
       setInterval(function(){  
         updateSubject(); 
         }, 2000);

         
   $("#add-subject-form").submit(function(evt){ 
    
       evt.preventDefault();
       
       var postData = $(this).serialize();  
       var url = $(this).attr('action');  
       $.post(url, postData, function(php_table_data){ 
     
            $("#subject-result").html(php_table_data);  
     });  
   
   });



function updateSubject(){

        $.ajax({
    
        url: 'display_subject.php',
        type: 'POST',
        success: function(show_subject){
        
            if(!show_subject.error) {
                
           $("#show-subjects").html(show_subject);
            
            
            }
        
        }
    
    });
}



});
 
 </script>
  


Syntax: add_subject.php



<?php
ob_start();
$connection = mysqli_connect('localhost', 'root', '','atnyla');


if(isset($_POST['subject_name'])) {
	
$subject_name = $_POST['subject_name'];
$query = "INSERT INTO subjects(Subject_Name) VALUES('$subject_name') ";
$update_subject_name = mysqli_query($connection, $query);
    
if(!$update_subject_name) {

die("QUERY FAILED");

}   
 
exit;


}

?>


Syntax: display_subject.php



<?php 
ob_start();
$connection = mysqli_connect('localhost', 'root', '','atnyla');

 	 $query = "SELECT * FROM subjects";
   	 $query_subject_info = mysqli_query($connection, $query);

        if(!$query_subject_info) {

        die("Query Failed" . mysqli_error($connection));


        }

        while($row = mysqli_fetch_array($query_subject_info)) {
            
           
            echo "<tr>";

            echo "<td>{$row['Subject_Id']}</td>";
            echo "<td>{$row['Subject_Name']}</td>";


            echo "</tr>";
            
        
        
        }


?>


🚀 More Blogs You Might Like

Explore more articles and keep learning

What is Bounce Rate in SEO? Complete Guide for Beginners
search-engine-optimization
What is Bounce Rate in SEO? Complete Guide for Beginners

Learn what bounce rate is in SEO, how it is calculated, why it matters, common causes of high bounce rates, an...

👁 28 2026-05-24
Read More →
Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 43 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 38 2026-05-19
Read More →
How to Grow Your Business Mindset Step by Step
skill
How to Grow Your Business Mindset Step by Step

Learn how to develop and grow a successful business mindset step by step. Discover entrepreneurial thinking, p...

👁 56 2026-05-09
Read More →