Advertisement
informaticage

Simple implementazione fetch scolastica

Jun 21st, 2023 (edited)
2,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. <html lang="en">
  2.  
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7. </head>
  8.  
  9. <body>
  10.     <header>
  11.         <h1> Elenco Aereoporti </h1>
  12.     </header>
  13.     <br>
  14.     <hr>
  15.     <br>
  16.  
  17.     <ul id="airports"></ul>
  18.  
  19.     <script>
  20.         (async () => {
  21.             const result = await fetch('http://localhost/esempio%20form/print_airports.php');
  22.             const data = await result.json();
  23.  
  24.             const airports = document.querySelector("#airports");
  25.             console.log("TONNO", airports)
  26.             for (const row of data) {
  27.                 airports.innerHTML += `
  28.                     <li>
  29.                         ID: ${row.id} <br>
  30.                         Name: ${row.name} <br>
  31.                         Code: ${row.code} <br>
  32.                         Location: ${row.location} <br>
  33.                     </li>
  34.                 <hr>
  35.                 `;
  36.             }
  37.         })();
  38.     </script>
  39.  
  40.     <style>
  41.         header {
  42.             text-align: center;
  43.         }
  44.  
  45.         header h1 {
  46.             font-family: sans-serif;
  47.         }
  48.  
  49.         ul li {
  50.             list-style-type: none;
  51.             margin: 10px;
  52.         }
  53.     </style>
  54. </body>
  55.  
  56. </html>
  57.  
  58.  
  59. <?php
  60.     header("Content-Type:application/json");
  61.     require_once('dbconnect.php');
  62.     $query = "SELECT id, name, code, location FROM AEREOPORTO";
  63.  
  64.     if ($result = $mysqli->query($query)) {
  65.         $data = $result->fetch_all(MYSQLI_ASSOC);
  66.  
  67.         echo json_encode($data);
  68.     }
  69.    
  70.  
  71. <?php  
  72.     require_once ('dbconnect.php');
  73.  
  74.     $id = $_POST['id'];
  75.     $name = $_POST['name'];
  76.     $code = $_POST['code'];
  77.     $location = $_POST['location'];
  78.  
  79.     $query = "INSERT INTO aereoporto (id, name, code, location) VALUES ($id, '$name', '$code', '$location')";
  80.  
  81.    
  82.     // Perform query
  83.     if ($result = $mysqli -> query($query)) {
  84.         echo "Created";
  85.     }
  86.    
  87.     $mysqli -> close();
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement