Problem when inserting data with ajax

1

My problem in itself is not inserting data, given that these are done successfully, the problem is that when you insert a data, it is repeated 2.3 or even 4 times in the database, another thing is that when entering the page and doing it for the first time, this makes it normal, but while this process is done as the 3 time of inserts data, they begin to repeat. I leave the code below to see if anyone can help me.

function insertar(){               
          $.ajax({            
              type : 'POST',
              url  : '../indemnizaciones/tramite/Tinsertar.php',
              data: $('#form').serialize(),
              cache: false,
              success :  function(data) {                 
                  $("#Tresul").html(data).fadeIn();},
              complete: function(){
                   setTimeout(function() {
                    $('#Tresul').fadeOut();}, 10000);}})};
<?php error_reporting(0); ?>
<div id="formulario">
<center>
    <div id="Tresul"></div>
</center><br>  
<form method="POST" id="tramite">
<input type="text" name="predio" style="display:none; " value="<?php echo $formulario['TPRE_IDPRE']; ?>">
<div class="row">
  <div class="col-md-3 col-md-offset-1" style="margin-left: 250px;">

      <label>Oficio notificacion</label><br>
      <input type="text" name="oficioN" style="height: 30px; width: 80%;" <?php if( $formularioT['TTRA_NONOT'] <> "") { ?> value="<?php echo $formularioT['TTRA_NONOT']; ?>" <?php } ?> ><br><br>

      <label>Oficio solicitud autorizacion</label><br>
      <input type="text" name="oficioS" style="height: 30px; width: 80%;" <?php if( $formularioT['TTRA_NSAUT'] <> "") { ?> value="<?php echo $formularioT['TTRA_NSAUT']; ?>" <?php } ?>><br><br>

      <label>Oficio autorizacion</label><br>
      <input type="text" name="oficioA" style="height: 30px; width: 80%;"  <?php if( $formularioT['TTRA_NOAUT'] <> "") { ?> value="<?php echo $formularioT['TTRA_NOAUT']; ?>" <?php } ?>><br><br>
        <br>
  </div>

  <div class="col-md-3 col-md-offset-1">
  
      <label>Fecha entrega notificacion</label><br>
      <input type="date" name="fechaN" style="height: 30px; width: 80%;"  <?php if( $formularioT['TTRA_FENOT'] <> "") { ?> value="<?php echo $formularioT['TTRA_FENOT']; ?>" <?php } ?>><br><br>

      <label>fecha solicitud autorizacion</label><br>
      <input type="date" name="fechaA" style="height: 30px; width: 80%;"  <?php if( $formularioT['TTRA_FSAUT'] <> "") { ?> value="<?php echo $formularioT['TTRA_FSAUT']; ?>" <?php } ?>><br><br>

      <label>fecha recepcion</label><br>
      <input type="date" name="fechaR" style="height: 30px; width: 80%;"  <?php if( $formularioT['TTRA_FAUTP'] <> "") { ?> value="<?php echo $formularioT['TTRA_FAUTP']; ?>" <?php } ?>><br><br>
  <br></div>
</div>
<center><br><center><br><button type="button" onclick="insertar();" id="boton">Guardar</button>

</form>
</div>

<?php

require("../../conexion.php");

 $predio = $_POST['predio']; 
 $oficioN= $_POST['oficioN']; 
 $oficioS = $_POST['oficioS']; 
 $oficioA = $_POST['oficioA']; 
 $fechaN = $_POST['fechaN']; 
 $fechaA = $_POST['fechaA']; 
 $fechaR = $_POST['fechaR']; 


 $comprobar="SELECT * FROM ttramite WHERE TTRA_IDPRE='$predio';";
 $compro=$mysqli->query($comprobar); 				


 if($compro->num_rows==0){

    $consulta = "INSERT INTO ttramite (TTRA_NONOT,TTRA_FENOT,TTRA_NSAUT,TTRA_FSAUT,TTRA_NOAUT,TTRA_FAUTP,TTRA_IDPRE) VALUES ('$oficioN','$fechaN','$oficioS','$fechaA','$oficioA','$fechaR','$predio');";

    if($resultado = $mysqli->query($consulta)){ 
	    echo "<h5 style='color:#06A322;'>Registro guardado correctamente</h5>";
    } else {
	    echo "<h5 style='color:#A30606;'>Imposible guardar, ocurrio un error</h5>";
    }
 }else{
 							
 	$consultaD="UPDATE ttramite SET TTRA_NONOT='$oficioN',TTRA_FENOT='$fechaN',TTRA_NSAUT='$oficioS', TTRA_FSAUT='$fechaA',TTRA_NOAUT='$oficioA',TTRA_FAUTP='$fechaR' WHERE TTRA_IDPRE='$predio';";


	if($resultadoD = $mysqli->query($consultaD)){
        echo "<h5 style='color:#0280BF;'>Registro modificado correctamente</h5>";
	} else {
         echo "<h5 style='color:#A30606;'>Imposible modificar, ocurrio un error</h5>";				    
	}
 }
 				
$mysqli->close();
?>
    
asked by Erik Raúl González Páez 10.09.2018 в 17:44
source

1 answer

0

The problem is that the data is repeated when you submit, the data of the form will be sent and then the $ .ajax the same Action, and that is why it has the problem of repeated data, the solution that I I propose is not to activate the submit of the form, and only use the $. ajax to enivar the data, you only have to change the type of button from submit to button, ejm: <button type="button"> and in the js rempse the submit event by click event of the button.

I hope your help is welcome!

    
answered by 10.09.2018 / 17:59
source