Advertisement
GeorgePashev_88

Web3 FullStack Students App HTML Final

Jan 4th, 2024
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.03 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Studenti Blockchain Interface</title>
  8.      <!-- Bootstrap CSS -->
  9.     <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
  10.     <!-- Optional: Bootstrap JS -->
  11.     <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  12.  
  13.  
  14.     <style>
  15.         table, th, td {
  16.             border: 1px solid black;
  17.             border-collapse: collapse;
  18.         }
  19.         td, th {
  20.             padding: 10px;
  21.         }
  22.     </style>
  23. </head>
  24. <body>
  25.     <h2>STUDENTI FRONT END</h2>
  26.     <p>Blockchain Front End for Studenti Contract</p>
  27.  
  28.     <form id="studentForm">
  29.         <input type="text" id="facNum" placeholder="Faculty Number" required>
  30.         <input type="text" id="firstName" placeholder="First Name" required>
  31.         <input type="text" id="lastName" placeholder="Last Name" required>
  32.         <input type="text" id="age" placeholder="Age" required>
  33.         <button type="submit">Add/Update Student</button>
  34.     </form>
  35.  
  36.     <h3>Students Table:</h3>
  37.     <div id="studentsTable"></div>
  38.  
  39.     <script>
  40.         document.getElementById('studentForm').addEventListener('submit', function(e) {
  41.             e.preventDefault();
  42.  
  43.             const studentData = {
  44.                 _facNum: document.getElementById('facNum').value,
  45.                 _firstName: document.getElementById('firstName').value,
  46.                 _lastName: document.getElementById('lastName').value,
  47.                 _age: document.getElementById('age').value
  48.             };
  49.  
  50.             fetch('http://127.0.0.1:5000/api/addOrUpdateStudent', {
  51.                 method: 'POST',
  52.                 headers: {
  53.                     'Content-Type': 'application/json'
  54.                 },
  55.                 body: JSON.stringify(studentData)
  56.             })
  57.             .then(response => response.json())
  58.             .then(data => {
  59.                 console.log(data);
  60.                 alert(data.message);
  61.                 document.getElementById('studentForm').reset();
  62.                 fetchStudents(); // Refresh the students table
  63.             })
  64.             .catch((error) => {
  65.                 console.error('Error:', error);
  66.             });
  67.         });
  68.  
  69.         function fetchStudents() {
  70.             fetch('http://127.0.0.1:5000/api/getStudents')
  71.                 .then(response => response.json())
  72.                 .then(data => {
  73.                     createStudentsTable(data);
  74.                 })
  75.                 .catch((error) => {
  76.                     console.error('Error:', error);
  77.                 });
  78.         }
  79.  
  80.         function createStudentsTable(data) {
  81.     const container = document.getElementById('studentsTable');
  82.     container.innerHTML = ''; // Clear existing content
  83.  
  84.     // Check if data is an array and has at least one element
  85.     if (Array.isArray(data) && data.length > 0) {
  86.        const table = document.createElement('table');
  87.         const header = table.insertRow();
  88.  
  89.         // Use the keys of the first object in the array for headers
  90.         const headers = Object.keys(data[0]);
  91.         headers.forEach(headerText => {
  92.             const headerCell = document.createElement('th');
  93.             headerCell.textContent = headerText.charAt(0).toUpperCase() + headerText.slice(1); // Capitalize the header
  94.             header.appendChild(headerCell);
  95.         });
  96.  
  97.         // Add rows for each student
  98.         data.forEach(student => {
  99.             const tr = table.insertRow();
  100.             headers.forEach(prop => {
  101.                 const td = tr.insertCell();
  102.                 td.textContent = student[prop];
  103.             });
  104.         });
  105.  
  106.         container.appendChild(table);
  107.     } else {
  108.         container.textContent = 'No students data available';
  109.     }
  110. }
  111.  
  112.  
  113.         // Initial fetch of students data
  114.         fetchStudents();
  115.     </script>
  116. </body>
  117. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement