Get Data From a Database Without Refreshing the Browser Using Ajax

In this tutorial, we are going to see how to get data from a database without refreshing the browser using Ajax.

Step 1: Create an HTML form to upload data

First we will create a simple form to get the user’s data. Create a page called “index.html” and paste the code below.

	    <script src=""></script>
	<script type="text/javascript">
		//Put the JQuery code here...
		<input type="text" name="name" id="name" onkeyup="getdata();">
		<div id="res">  </div>

When the user enters the name on the input, and clicks the Enter key, the onkeyup event occurs by calling the getdata() method.

Step 2: Get data with Ajax/jQuery

We will use the AJAX method to get the data without refreshing the page.

function getdata()
	 var name = document.getElementById("name");
			type: 'post',
			url: 'getdata.php',
			data: {
			success: function (response) {
		$('#res').html("Enter the user's name");

Step 3: Connect to the database and get data

In this step, we query the database and get the desired results.

// File : getdata.php

	if( isset( $_POST['name'] ) )
		$name = $_POST['name'];

		mysql_connect('localhost', 'root', ' ');


		$data = " SELECT age FROM user WHERE name LIKE '$name%' ";

		$query = mysql_query($data);

		while($row = mysql_fetch_array($query))
		   echo "<p>".$row['age']."</p>";

In this tutorial, we have seen how to get data from a database without refreshing the browser using Ajax. You can further customize this code according to your needs.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *