How to reload only table after ajax

Asked

Viewed 166 times

0

I am reloading the page after entering a data in the bank via ajax, there is the possibility to update ( rebuild) only the table, or the div in which it is?

$(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();
});

});

  • 2

    edits and inserts what is inside #formcadastro

  • You can manipulate the DOM and add in your table, put an ID on it to simplify see here: https://stackoverflow.com/questions/171027/add-table-row-in-jquery

  • There’s no way to rebuild ?

  • just return the html for a call in a separate file or a specific treatment in your php to return only the piece that fills the table, and then replace the html of the div. There are many ways to do it that you are wanting.

1 answer

0

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

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

php table.

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

script js.

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

That way when starting the document jQuery will load all the contents of php table. on your table.

Browser other questions tagged

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