How to show data using ajax in php?

How to show dynamic data in php using ajax?


Database Connection

File Name : database.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="itechxpert";
$conn = mysqli_connect($servername, $username, $password,$db);
?>

View Page

File Name : index.php

<!DOCTYPE html>
<html lang="en">
<head>
<title>How to show dynamic data in php using ajax</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>How to show dynamic data in php using ajax</h2>
<table class="table table-bordered table-sm" >
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>mobile</th>
<th>City</th>
</tr>
</thead>
<tbody id="view_data">

</tbody>
</table>
</div>
<script>
$(document).ready(function() {
$.ajax({
url: "View_ajax.php",
type: "POST",
cache: false,
success: function(data){
alert(data);
$('#view_data').html(data);
}
});

});
</script>
</body>
</html>

Ajax page

File Name : View_ajax.php

<?php
include 'database.php';
$sql = "SELECT * FROM itechxpert";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?=$row['name'];?></td>
<td><?=$row['email'];?></td>
<td><?=$row['mobile'];?></td>
<td><?=$row['city'];?></td>
</tr>
<?php
}
}
else {
echo "0 results";
}
mysqli_close($conn);
?>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here