Site Loader
Get a Quote

Php script to insert records in MySQL using Jquery and Ajax

Insert records in MySQL using Jquery and Ajax: Hi readers today we come up with the topic of Ajax(Asynchronous Javascript and XML). Here in this tutorial, we have provided the short description about AJAX and then the detail with full source code of Php script to insert records in MYSQL using Jquery and Ajax.

AJAX is a short form of Asynchronous Javascript And XML(Extended Markup Language) . Here Asynchronous simply means that at the time of browser request the user is not blocked and can perform another operation also.

So Ajax generally used to create an Asynchronous web application on client side i.e, send or receive data from server without page reload or Refresh

Here is the step-by-step guide to inserting records in Mysql using Jquery and Ajax.


Step1: First, Create a Database and table directly from the PHPMyAdmin 

open localhost/phpmyadmin and open sql tab and run the below sql query command

Create Database session;

create table register(
name varchar(30),
username varchar(30) NOT NULL,
password varchar(10) NOT NULL,
email varchar(30) NOT NULL,
status int(11),

Step 2: After table creation create a Registration Form using PHP script

Here we have created the registration form with only “id” whereas the attributes like action and method is given through AJAX. Including the created js file under <head> tag.

<!Doctype html>
<head><title>Registration Form</title>

//including jquery script
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/postformvalidation.js"></script>
<!--Create a Registration form with id -->
<form id="registrationForm">
<div class="margin">
Enter Name:
<input type="text" id="name" name="name" />
<div class="margin">
Enter Username:
<input type="text" id="username" name="username"/>
<div class="margin">
Enter Password:
<input type="password" id="password" name="password"/>
<div class="margin">
Enter E-mail Id:
<input type="email" id="email" name="email" />
<div class="margin">
<input type="submit" id="Register" name="Register" value="submit"/>

//creating a div element to display the result
<div id="result"></div>
<div id="show"></div>
<!--end of form-->



Step 3: create a .js file to insert records using Jquery and Ajax code

Here we have created the JavaScript file named “postformvalidation.js”. First, use the $(document).ready function to make the DOM element to be fully loaded and then use a click() function to create an event on form i.e when the submit button of form is clicked by user then it checks all the validation and required field afterwards all the data is passed through Ajax by using the ajax() jquery function 

//ajax function is called
type:"POST",                       //method type of form
url:"register.php",                //action attribute of form
data:formData,                     //data is stored
success:function(result){           //success is used to display result

//when registration form submit button is clicked
 $("#registrationForm #Register").click(function(){

//required field:----

 var name=$("input#name").val();
 $("#error").fadeIn().text("Name Required");
 return false;
 var username=$("input#username").val();
 $("#error").fadeIn().text("User name is Required");
 return false;

 var password=$("input#password").val();
 $("#error").fadeIn().text("password Required");
 return false;
 var email=$("input#email").val();
 $("#error").fadeIn().text("Email id is Required");
 return false;
 //temporary variable to store data from form
 var formData='name='+ name+ '&username=' + username+ '&password=' + password +'&email='+email;
 if(name==''||username==''|| password==''||email=='')
 alert("Please Fill All Fields");
 //ajax function is called
 return false;


Step 4: create a PHP file to write script of insert into and select command in MYSQLi

First, create a PHP file named register .php and copy the below code and then save it. Here in this Php file, we have created the connection with MYSQLi and then run the Insert into command using a php script to insert records successfully using MySQL procedural function then fetch the result using a SELECT * command.This Page will insert records in MYSQL using Jquery and Ajax.

//create a connection with database session 
$conn = mysqli_connect("localhost","root","","session");
//check the connection
if (mysqli_connect_errno())
echo 'MySQLi Connection was not established: ' . mysqli_connect_error();

//insert records in register table in session database
$create="insert into register(name,username,password,email,status,last_login) 
//run the above query using mysqli_query() function
 die("error in inserting records:".mysqli_error());
 echo "Records successfully inserted into table";
//display records 
$sql="select * from register";

 //display records in table
 echo "<table><tr><th>ID</th><th>Name</th><th>Username</th><th>Password</th>
 <th>email</th><th>Status</th><th>Last_Login</th><th>Delete Records</th></tr>";


//display each records 
 echo "<tr><td>".$rows["id"]."</td><td>".$rows["name"]."</td><td>".$rows["username"]."</td><td>".$rows["password"]."</td><td>".
 echo "</table>";

//close the connection


<!--php code ends-->

Snapshot view:

insert records in MYSQL using Jquery and Ajax
insert records in MYSQL using Jquery and Ajax

I hope you would like this tutorial to insert records in mysql using Jquery and ajax!

Post Author: TechieFlair

Leave a Reply

Your email address will not be published.