Refresh the content of a DIV

0

hello friends I have a registration form with a div below that shows all the records the idea is that when someone registers I refresh that record div with a function or something like what I do not want to actually have to bring back those data that are already from ajax any idea or tips thanks in advance

php code

<div class="body">
     <div>
      <form method="post" id="form_add">          
              <input type="text" class="form-control" id="txt_medidas" 
               <button type="submit" class="btn btn-primary">+</button>  
      </form>

      <div id="tabla_datos"> 
            <?php
            ...
            foreach( $reg) { echo $reg->dato(); }
            ...
            ?>    
      </div> 
     </div>

javascript code

  $(document).ready(function(){ 
      $("#form_add").submit(function(){
        $.ajax({    
          url: "post_add.php",
          type: "POST",
          data: $("#form_add").serialize(),
          success: function(data){
            if(data){
              alert('OK');

            }else alert('Error al guardar');  
          }        
        }); 
        return false;
      });
  });
    
asked by andy gibbs 30.05.2018 в 16:09
source

2 answers

1

I recommend using this library Datatablejs with the help of jquery and few lines of code you can create a dynamic table where you have functions like $('tabla').DataTable().ajax.reload(); and you can add it to your ajax in this way:

  $(document).ready(function(){
    $("#form_add").submit(function(){
      $.ajax({    
        url: "post_add.php",
        type: "POST",
        data: $("#form_add").serialize(),
        success: function(data) {
         if(data){
          alert('OK');
          $('tabla').DataTable().ajax.reload();
         } else {
          alert('Error al guardar');  
         }        
      }); 
    return false;
   });
  });
    
answered by 30.05.2018 / 16:36
source
1

In the success of your ajax you can add code html in a div that printas in the url where you send the data.

 $(document).ready(function(){ 
          $("#form_add").submit(function(){
            $.ajax({    
              url: "post_add.php",
              type: "POST",
              data: $("#form_add").serialize(),
              success: function(data){
                if(data){
                  alert('OK');
                  $("#datosamostrar").html(data);

                }else alert('Error al guardar');  
              }        
            }); 
            return false;
          });
      });

And in your php you create the div where you are going to put that data where you want it to appear:

<div id="datosamostrar"></div>
    
answered by 30.05.2018 в 16:19