PHP MySQL Insert Into Database Without Refreshing Page

Hello friends, in this PHP MySQL Insert into Database tutorial we will use jQuery AJAX method to insert values in our MySQL database without refreshing our web page. So lets begin. PHP MySQL with AJAX …

Hello friends, in this PHP MySQL Insert into Database tutorial we will use jQuery AJAX method to insert values in our MySQL database without refreshing our web page. So lets begin.

PHP MySQL with AJAX Tutorial

  • You can read this tutorial or you can watch this playlist for complete detailed explanation.

What you need before starting with PHP MySQL Insert Tutorial?

  • WAMP / XAMPP server (I will be using wamp)
  • Notepad++ (notepad is also ok)
  • Little knowledge of PHP, SQL and JavaScript

Creating Database

  • First we will create a database
  • Go to phpmyadmin (localhost/phpmyadmin)
  • Create a database
  • Create a table inside the database named users
php mysql insert
Creating Database

I have created a table name users. In users I have three columns (id, username and password) as you can see above. Id is primary key with an auto increment. So we will insert only username and password with our html form.

Creating HTML Form to Insert Values in Database

  • Now we will create our html form.
  • You can use the below html code to create your form.

  • As you can see we have one form with action = insert.php i.e. when we click on the button we will be redirected to insert.php.
  • The method is post and we have also given an id to our form which is myform.
  • The form has two inputs, one for username and other for password with name and id attribute.
  • We will use the id attribute to access the values from jQuery. So we have also added the jQuery file inside the head tag.
  • And at last we have a p element with an id result. This is to show the success message after successful insertion.
  • The above form will generate following output.
php mysql insert tutorial
index.html

Creating PHP Script to Insert Values in Database

  • Create a file named insert.php
  • First we will connect to our database.
  • For connecting we will define some constants.

  • Now we will connect to our database using the constants we defined.

  • Get the values which are coming from our form using post method.

  • Finally we will insert these values into our database

  • Your final php script for php mysql insert project is

  • Now try running your project.
  • It is working fine but we are getting redirected to insert.php. Our task is to insert without refreshing our redirecting the page. For this we will use jQuery AJAX method.

Using jQuery AJAX Methods to insert values in database without refreshing the page

  • Create a new file name insert.js
  • Add your insert.js to your html file below the form

  • First we will stop our form from getting redirected to insert.php

  • Try clicking insert button now, nothing will happen. 😛
  • Now we will execute our php script in background using jQuery post method.

  • We will use the above post method inside the click function of our button
  • So our final insert.js will be

  • Try running your project now. It is working fine the values are getting inserted in database without refreshing our page.
  • You can also download the project source from the link given below. And if you are having any confusion or doubts following the steps feel free to comment. Thank You 🙂

[sociallocker id=”1372″] [download id=”1400″] [/sociallocker]

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x