THE INDIA LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Web Building - Data as a Service (DaaS)


Building a web site from scratch.   Part VIII: Data as a service.


What We Will Do

In this chapter we will:

  • Fetch dynamic data from a web server running SQL

Using a PHP Server Running MySQL

In the demoweb folder, change the file customers.html.

Put the following code inside the file:

customers.html

<!DOCTYPE html>
<html>

<head>
  <title>Customers</title>
  <link href="site.css" rel="stylesheet">
</head>

<body>

<nav id="nav01"></nav>

<div id="main">
  <h1>Customers</h1>
  <div id="id01"></div>
  <footer id="foot01"></footer>
</div>

<script src="script.js"></script>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "https://www.noidatut.com/website/customers_db_mysql.php";

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var obj = JSON.parse(response);
    var arr = obj.records;
    var i;
    var out = "<table><tr><th>Name</th><th>City</th><th>Country</th></tr>";
    for(i = 0; i < arr.length; i++) {
        out += "<tr><td>" +
        arr[i].Name +
        "</td><td>" +
        arr[i].City +
        "</td><td>" +
        arr[i].Country +
        "</td></tr>";
    }
    out += "</table>"
    document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>
Try it Yourself »

The code above is the same as in the JSON chapter.

Only this time the XMLHttpRequest reads data from "customers_db_mysql.php".

customers_db_mysql.php fetches data from a "live" database, while customers.php fetches a "static" text file.


Using an ASP.NET Server Running SQL Server

In the demoweb folder, change the file customers.html.

Put the following code inside the file:

customers.html

<!DOCTYPE html>
<html>

<head>
  <title>Customers</title>
  <link href="site.css" rel="stylesheet">
</head>

<body>

<nav id="nav01"></nav>

<div id="main">
  <h1>Customers</h1>
  <div id="id01"></div>
  <footer id="foot01"></footer>
</div>

<script src="script.js"></script>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "https://www.noidatut.com/website/customers_db_sql.aspx";

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var obj = JSON.parse(response);
    var arr = obj.records;
    var i;
    var out = "<table><tr><th>Name</th><th>City</th><th>Country</th></tr>";
    for(i = 0; i < arr.length; i++) {
        out += "<tr><td>" +
        arr[i].Name +
        "</td><td>" +
        arr[i].City +
        "</td><td>" +
        arr[i].Country +
        "</td></tr>";
    }
    out += "</table>"
    document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>
Try it Yourself »

The code above is the same as the previous one.

Only this time the XMLHttpRequest reads data from "customers_db_sql.aspx".


Read More

Read more about SQL in our SQL Tutorial.