when inserting form with ajax does not refresh fromulario

0

I created an insert with ajax and it does it perfectly in the database but the form does not refresh or does not return to its origin ... the record is inserted but it enters several copies of record when inserting the button or when doing , what can it be?

<script type="text/javascript">
	$(document).ready(function(){
		$('#insertar').click(function(e){
            e.preventDefault();
			var datos=$('#formulario1').serialize();
			$.ajax({
                type:"POST",
                url:"insertar.php",
                data:datos,
                async: true,
				success: function(data){
                    $('#respuesta').html(data);
                },
                error: function() {
                    $('#respuesta').html(data);
                },
                complete: function(){
                       setTimeout(function() {
                            $('#respuesta').fadeOut();
                       }, 2000);    
                    }
			});
            return; 
		});
	});
        

</script>
<form id="formulario1">
  <div class="form-group">
    <label for="nombrelibro">Nombre</label>
    <input type="text" class="form-control" id="nombrelibro" name="nombrelibro" aria-describedby="nombrelibro" placeholder="Inserte nombre Libro">
    <small id="nombrelibro" class="form-text text-muted">Libro sin autor .</small>
  </div>
  <div class="form-group">
    <label for="autor">Autor libro</label>
    <input type="text" class="form-control" id="autor" name="autor" placeholder="Indique Autor Libro">
  </div>
  <div class="form-group">
 <label for="codigo">Codigo</label>
    <input type="text" class="form-control col-md-3" id="codigo" name="codigo">
    
  </div>
  <button type="submit" class="btn btn-primary" id="insertar">Insertar</button>
</form>
      <div id="respuesta"></div>
    
asked by actio 21.09.2018 в 05:27
source

1 answer

0

The problem you have is that by having the form with a submit button, this one executes an action and then the other ajax.

The solution would be to remove one of the two options. If you want to do it with ajax, you have to change the type of the button.

<button type="button" class="btn btn-primary" id="insertar">Insertar</button>

And in case you want to do them with the form, you have to add the action and the method to the form.

<form id="formulario1" action="insertar.php" method="POST">

In the latter case, the button would be of the submit type.

    
answered by 21.09.2018 в 10:09