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>