How to reload only table after ajax

0

I am reloading the page after inserting a data into the database via ajax, is it possible to update (rebuild) only the table, or the div it is in?

$(document).ready(function(){
$('#cadastrouser').hide();

$('#formcadastro').submit(function(event) {

    // process the form
    $.ajax({
        type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
        url         : 'cadastro.php', // the url where we want to POST
        data        : $("#formcadastro").serialize(), // our data object
        dataType    : 'json', // what type of data do we expect back from the server
        encode      : true
    })
        .done(function(data) {
            var resdata = data;
            if (resdata['sucesso']) {
                location.reload();
            }else{
                alert(resdata['mensagem']);
            }
        });
    event.preventDefault();
});

});

    
asked by anonymous 12.12.2017 / 18:17

1 answer

0

Yes, if I understood your question, it would be like dynamically updating / building a table / div with AJAX, you can do it as follows.

Store in a file the content that will be displayed in your example table:

table.php

<table>
    <tr>
        <td>conteúdo</td>
    </tr>
</tabela>

script.js

$(document).ready(function() {
    $("table").load("tabela.php");
});

In this way, when you start the document, jQuery will load all the contents of table.php into your table.

    
13.12.2017 / 14:09