I have tried everything and it seems that the form is submitted twice only when pressing enter or using a barcode scanner which I want to use. I does not happen when clicking the "submit button". At first I thought I was executing the query twice but it's not the case. This is my code:
HTML file
<form id="formulario_ingreso" class="form-horizontal" onsubmit="return ingresar();" method="POST">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div class="controls">
<input id="codigo" type="number" name="codigo" class="form-control" placeholder="Código" autofocus>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<select class="select2 form-control" id="nombre" name="nombre">
<option value="">Nombre (Solo si no existe código)</option>
<?php
while ($row = mysqli_fetch_assoc($perfiles_query)){
echo '<option value="'.$row["id"].'">'.$row["nombres"].' '.$row["apellidos"].'</option>';
}
?>
</select>
</div>
</div>
</div>
<button type="button" class="btn btn-primary" onclick="ingresar()">Ingresar</button>
</form>
SCRIPT file
<script>
$("#formulario_ingreso").submit(function(e) {
e.preventDefault();
});
</script>
<script>
function ingresar(){
var codigo = $("#codigo").val();
var nombre = $("#nombre").val();
if(codigo){
$.post("evento/ingresar_asistencia.php", { codigo: codigo },
function(data) {
$('#results').html(data);
$('#formulario_ingreso')[0].reset();
});
}else if(nombre){
$.post("evento/ingresar_asistencia.php", { nombre: nombre },
function(data) {
$('#results').html(data);
$('#formulario_ingreso')[0].reset();
});
}
};
</script>
PHP FILE
<?php
require_once '../../../config.php';
if(isset($_POST["codigo"])){
$codigo = $_POST["codigo"];
echo $codigo;
$asistencia_query = mysqli_query($link, "INSERT INTO asistencia (id_perfil, id_evento) VALUES (2,1) ");
}
I have tried your code but in my case, it's a one-time call ingresar()
function when I click on the button.
Please remove onsubmit="return ingresar();"
from you form tag. I think this one is creating the issue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.