Return a JSON table using Element creation (I cannot use Jquery)

Asked

Viewed 38 times

1

Instead of returning the table in html, return in JSON and mount the table using the element creation (Document.createelement) and insertion and removal features of the DOM tree.

I need a help with that question, you can help me ?

Currently my code is like this:

<table class="table table-dark table-striped">
<thead>
  <tr>
    <th scope="col">#id_armas</th>
    <th scope="col">Nome Arma</th>
    <th scope="col">Ação</th>
  </tr>
</thead>
<?php
include 'conexao.php';
$sql = "SELECT * FROM `armas`";
$busca = mysqli_query($conexao, $sql);

while ($array = mysqli_fetch_array($busca)) {
  $id_armas = $array['id_armas'];
  $nomearma = $array['nome_arma'];
  ?>
  <tr>
    <td><?php echo $id_armas ?></td>
    <td><?php echo $nomearma ?></td>
    <td><a class="btn btn-primary btn-sm" style="color:#ffffff" href="editar_armas.php?id=<?php echo $id_armas ?>" role="button"><i class="fas fa-pencil-alt"></i>&nbsp; &nbsp; Editar</a>
      <?php if ($_SESSION['nivel'] == 1) { ?>
        <a class="btn btn-danger btn-sm" style="color:#ffffff" data-confirm='Tem certeza que deseja apagar esse item?' href="deletar_armas.php?id=<?php echo $id_armas ?>" role="button"><i class="fas fa-trash-alt"></i>&nbsp; &nbsp; Apagar</a>
      <?php } ?></td>
  </tr>
<?php
  //abrindo novamente a tag php e fechando a {} do wilhe apos o td pois o laco vai adicionar elementos ao TD.
} ?>
</tr>

and need to do this only in json and adding the table in the dom tree

    var ajax = new XMLHttpRequest();
  ajax.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var table = document.getElementById('tabela');
      var tr = document.createElement('tr');

      table.appendChild(tr);

      var td = document.createElement('td');
      tr.appendChild(td);
      td.innerHTML = 'teste';
      var td = document.createElement('td');
      tr.appendChild(td);
      td.innerHTML = 'teste';
    }
    ajax.open("GET", "conteudotab.php");
    ajax.send();
  }
  • You want to create the whole table or just the PHP lines?

  • Welcome to Stack Overflow! Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of this. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English.

  • I want to create the whole table, that whenever a record is added in the database it creates

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.