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.
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.
PHP MySQL Insert into Database without Refreshing
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

Creating Database
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.

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
Comments
Post a Comment