How to retrieve data from database in PHP using AJAX?

Hello friends, In the last post we have inserted values to our without refreshing our page. In this post we will learn how to retrieve data from database in php using ajax jquery method. We will …

how to retrieve data from database in php using ajax

Hello friends, In the last post we have inserted values to our without refreshing our page. In this post we will learn how to retrieve data from database in php using ajax jquery method. We will fetch data from database without refreshing the webpage. So lets begin but before if you haven’t read the last post you can read it from here.

How to insert values to database in PHP using AJAX?

How to retrieve data from database in PHP using AJAX

Before going through the tutorial you can download the source code from the link given below.

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

Creating Database Table

  • First create a table inside your database. I have created a simple table named products as you can see in the below image.
how to retrieve data from database in php using ajax
Database Table
  • You also need to insert some values in your table as you can see above.

Creating HTML Page

  • Now we will create a simple html page with two dropdowns. In first dropdown we will see the brand list. When we change the brand list the other drop down will be loaded automatically by fetching the value from database.
  • You can use the following html code to create your page. Copy and save it as index.html in your project folder.

Creating PHP Script

  • Create a new php file named fetch.php in your project folder.
  • Copy the following code.

  • The above code is very simple. We are pushing all the records to an array. Finally we are displaying that array in json format. We will read this JSON using jQuery.

Finally Creating jQuery Script to Make it Work

  • Now we need execute our script using jQuery to get the JSON data.
  • We will change function in our select element. Use the following code

  • Copy and save the above code as fetch.js
  • And include the script to your html page.
  • Try executing your project now.

So thats it for this post about how to retrieve data from database in PHP using AJAX. If you had any confusion in my code or trouble in following the steps feel free to ask by your comments. Thank You 🙂

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