N
N
Natalia2018-05-25 05:41:56
PHP
Natalia, 2018-05-25 05:41:56

How to output data from JS to a table?

Getting data from the database

$users=get_users();
echo json_encode($users);

function get_users(){
  global $db;
  $query = "SELECT  t.ID_Teacher,t.Surname,t.Name,t.Patronymic
                    FROM Teachers t
                         LEFT JOIN Users u
                         ON t.ID_Teacher=u.ID_User";
  $result = mysqli_query($db, $query);
  $data = null;
  while($row = mysqli_fetch_assoc($result)){
    $data[] = $row;	
  }
  return $data;
}


I output data without rebooting
function getStudents() {
        $.ajax({
          url: 'getUser.php',
          dataType: "json",
        }).done(function(studentsList ){
          var table = document.getElementById('tab1');
          

          
          var studentsHTML = "";
          

          for(var i=0; i<studentsList.length; i++){
            
            studentsHTML += studentsList[i].Surname;
            studentsHTML += " "+studentsList[i].Name;
            studentsHTML += " "+studentsList[i].Patronymic;
            studentsHTML += " <a href=\"#\" "+"onclick=\"deleteStudent("+studentsList[i].ID_Teacher+"); return false;\">Удалить</a>";
            studentsHTML += "<br />";
          }
          $("#students").html(studentsHTML);
        }).fail(function(){
          alert('Ошибка запроса к серверу!');
        });
      }
      getStudents();


5b0777f30035f023923042.pngHow to display data in a table?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Tokarchuk, 2018-05-25
@malishkina2502

var studentsHTML = "<table><tr><th>#</th><th>Фамилия</th> ... ";

studentsHTML += "<td>" + studentsList[i].Surname + "</td>”;

Etc.

A
Anatoly Medvedev, 2018-05-25
@balamyt92

https://webref.ru/course/html-content/tables

M
Mysterion, 2018-05-25
@Mysterion

You can form a table directly after receiving data from the database, give it and insert it into JS as html code somewhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question