How to add a row to a table using AJAX

I am currently studying the AJAX technology and a difficulty has arisen. I want to add a row in the table when I click on the button, but I don't quite understand how. It adds an entry to the database, but does not update the table, or it completely reloads the page, or you need to update it yourself. AJAX the code is shown below. Please do not criticize too much, I am a beginner and I am learning tirelessly: 3 Thank you in advance

$.ajax({
  url: 'add_user.php',
  type: 'POST',
  data: {
    "log": log,
    "pas": pas
  },
  success: function(respond, status, jqXHR) {
    if (typeof respond.error === 'undefined') {
      // var html = '';
      $.each(respond.files, function(key, val) {
        $('tr.').prop('val', val);
        var html = "<tr><td class='edit_log' name='login' id='login'>log</td><td class='edit_pas' name='pas' id='pas'></td></tr>";
        location.reload();
      })
    }
    // error
    else {
      console.log('ОШИБКА: ' + respond.error);
    }

  },
  error: function() {
    console.log('ОШИБКА AJAX запроса: ');
  }
});

1 answers

I don't understand why you need each and you do the relod yourself. In success, after checking for errors, just add the html code you need to the end of the table. If it is only one line, you can generate the html code on the server and return the finished line immediately. And then in success, add it to the table

 $(".table").append(html);
 0
Author: Дмитрий Анисько, 2020-01-13 21:15:26