জাভাস্ক্রিপ্ট অবজেক্ট নটেশন উদাহরণ (JSON Example)

এই উদাহরণটি পিএইচপি এবং মাইএসকউএল দ্বারা চালিত ওয়েব সার্ভার থেকে জেএসওএন ডাটা পড়ে।


<!DOCTYPE html>
 <html>
 <body>
 
 <h1>Customers</h1>
 <div id="id01"></div>
 
 <script>
 var xmlhttp = new XMLHttpRequest();
 var url = "http://www.w3schools.com/website/customers_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 arr = JSON.parse(response);
     var i;
     var out = "<table>";
 
     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>

সার্ভার এ পিএইচপি কোড

সার্ভারে এই পিএইচপি কোড চালিত  হয়


<?php
 header("Access-Control-Allow-Origin: *");
 header("Content-Type: application/json; charset=UTF-8");
 
 $conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
 
 $result = $conn->query("SELECT CompanyName, City, Country FROM Customers");
 
 $outp = "[";
 while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
     if ($outp != "[") {$outp .= ",";}
     $outp .= '{"Name":"'  . $rs["CompanyName"] . '",';
     $outp .= '"City":"'   . $rs["City"]        . '",';
     $outp .= '"Country":"'. $rs["Country"]     . '"}'; 
 }
 $outp .="]";
 
 $conn->close();
 
 echo($outp);
 ?>

 

স্টাইল ভার্সন

Customers.html


<!DOCTYPE html>
 <html>
 
 <head>
 <style>
 h1 {
     border-bottom: 3px solid #cc9900;
     color: #996600;
     font-size: 30px;
 }
 table, th , td {
     border: 1px solid grey;
     border-collapse: collapse;
     padding: 5px;
 }
 table tr:nth-child(odd) {
     background-color: #f1f1f1;
 }
 table tr:nth-child(even) {
     background-color: #ffffff;
 }
 </style>
 </head>
 
 <body>
 
 <h1>Customers</h1>
 <div id="id01"></div>
 
 <script>
 var xmlhttp = new XMLHttpRequest();
 var url = "http://www.w3schools.com/website/customers_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 arr = JSON.parse(response);
     var i;
     var out = "<table>";
 
     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>

 

 

Permanent link to this article: http://bangla.sitestree.com/%e0%a6%9c%e0%a7%87%e0%a6%8f%e0%a6%b8%e0%a6%93%e0%a6%8f%e0%a6%a8-%e0%a6%89%e0%a6%a6%e0%a6%be%e0%a6%b9%e0%a6%b0%e0%a6%a3/

Leave a Reply