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.

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 🙂

33 thoughts on “How to retrieve data from database in PHP using AJAX?”

  1. That’s very good a post you have there, what if I want to pull more than one data from the database table?

    Can this run simultaneously with another Ajax post, I mean like in a comment box where comments goes into the db when submitted and gets retrieved almost immediately?

    Reply
  2. the code which you have been posted is really good but i want the second data displayed in input field instead of select option. i have tried to change to input but it is not working please help me in ths

    Reply
  3. hi sir, pls help in php development.
    There are two tables in one database, in both the table one field name is common. In table1 inserting data (name) will not take repeated and in table2 inserting data (name) will be repeated as how many times we r inserting. Now if i’m inserting data in table2 field contains name, price, size. Automatically price should get added in table1 also but by checking name i.e, table2 name should be same as table1 name. And if the price value is 0 in table1 of name(xzy), when inserting data(price as 100) in table2 by checking name(xzy) in table1 price will get changed to 100 and same when inserting data(price as 200) in table2 by checking name(xzy) in table1 price will get changed to 300.

    Please help to do this functionality using php, mysql and javascript

    Reply
  4. Hi Belal,

    How would you fit this code to this concept: I would like data to be automatically displayed in a textview twhen the page is opened, therefore rather through a onCreate than onClick?

    Thanks in advance!

    Reply
  5. hello sir

    i currently working online examination project, i have some project related issue.

    when i add questions and options, right ans value 1 is store on top in database. how to store right ans value.

    Reply
  6. Hi Sir,

    I want to insert the data in database which page is already displaying through onLoad() function. when I submit the data and clicking the save button that is inserting in database but that page is going on another page.
    Example:– I have displayed the save.php through onload() profile.php page. after displaying save.php page that is working for insetion in database but that page will go on save.php but I want to hold on profile.php. I have no idea how to do.

    please help me.

    Thanks

    Reply
  7. Hello, i need your help for php code, 3 drop down menu- 1st drop down = state, 2nd drop down=district, 3rd drop down= city/town, please tell me how to work with mysql one database and one table, using ajax loader.

    Reply
        • Select Blood Group *

          Select Blood Group
          A+
          A-
          B+
          B-
          O+
          O-
          AB+
          AB-
          A1+
          A1-
          A2+
          A2-
          A1B+
          A1B-
          A2B+
          A2B-

          Select State *

          Select State
          ANDAMAN & NICOBAR ISLANDS
          ANDHRA PRADESH
          ARUNACHAL PRADESH
          ASSAM
          BIHAR
          CHANDIGARH
          CHATTISGARH
          DADRA & NAGAR HAVELI
          DAMAN & DIU
          DELHI
          GOA
          GUJARAT
          HARYANA
          HIMACHAL PRADESH
          JAMMU & KASHMIR
          JHARKHAND
          KARNATAKA
          KERALA
          LAKSHADWEEP
          MADHYA PRADESH
          MAHARASHTRA
          MANIPUR
          MEGHALAYA
          MIZORAM
          NAGALAND
          ODISHA
          PONDICHERRY
          PUNJAB
          RAJASTHAN
          SIKKIM
          TAMIL NADU
          TELANGANA
          TRIPURA
          UTTAR PRADESH
          UTTARAKHAND
          WEST BENGAL

          Select District *

          Select District

          Select Location *

          Select City / Town

          1st option= blood group
          2nd option=state (mysql database table contrypincode, data same)
          3rd option=district( 2nd option select, onchange 3rd option)
          4th option=city(3rd option select, onchange 4th option)

          batabase(contrypincode-table)
          id,
          statename,
          districtname,
          citytown

          Reply
  8. please help to solve this.. I have one form with 4 input tag and 2 buttons Save and Edit And One grid in that i fetch data from database. But Now i Want when i click any one row from grid i want click event to go in database and fetch data and fill all field present on form but that not working for me, i havn’t got one solution please help me

    Reply
  9. i am also a MCA student and always try to keep it simple and less as well as possible.
    it was very easy and accurately coded.

    Thanks dear

    Reply
  10. Hello. This code is working fine. Thank you. actually i’m working on a project where I’ve to fetch latitude and longitude from a database. Using this code I got an output which has a string of lat long in it. Now i want to split them. can you please help me with this? How should i take the output in a variable? As in your code, $(‘#item’).append displays the result. How to take this result into a variable ?

    Reply
  11. Please help me, i am new to php

    I need to display student registered details one by one in form from database by clicking on next button.
    I created the registeration form and inserted details into database,but i am not aware to view each student details one by one using next button.

    Reply

Leave a Comment