Wednesday, 21 April 2021

AJAX Web Applications with Database Access

 Cricket Score Display using AJAX

score.html

<!DOCTYPE html>
<html>
<head>
<script>
function viewScore()
{
request=new XMLHttpRequest();
request.open("GET","dbaccess.php",true);
request.send();
try
{
request.onreadystatechange=function()
{
if(request.readyState==4 & request.status=200)
{
document.getElementById('score').innerHTML=request.responseText;
}
};

}
catch(e)
{
alert("Unable to connect to server");
}
}
setInterval(viewScore,1000);
</script>
</head>
<body>
<h1>Retrive data from database using AJAX</h1>
<div id="score"><h2></h2></div>
</body>
</html>



dbaccess.php

<?php
// Create connection
$conn = new mysqli("localhost","abcd","1234", "test");

// Check connection
if ($conn->connect_error) 
{
    die("Connection failed:".$conn->connect_error);
}
$sql = "SELECT * FROM IPL";
$result = $conn->query($sql);
if ($result->num_rows > 0) 
{
    // output data of each row
    while($row = $result->fetch_assoc()) 
    {
        echo "Score:".$row["score"]."/".$row["wicket"]." Overs:".$row["over"];
    }
else 
{
    echo "0 results";
$conn->close();
?>