working with PHP and jquery.
It turns out that I am validating the information sent from a form. the validation that my model does to me returns 3 results: ok or duplicate field or not null. I receive the result:
- a) if it is ok (I show my message all ok).
- b) if it is duplicated (I show my message) and I put the focus on my field that has the duplicate field (which is only one) .(here the problem, the focus is put on the input but immediately the send button steals the focus ).(happens as soon as the sweet alert closes)
- c) the same for the null result.
<form method="POST" id="FormingreasarStand" enctype="text/plain">
<input id="iptnroStand" name="nroStand" placeholder="ejem: 56" type="number" class="form-control" min="1" max="10000" required autofocus>
<input id="iptNPAsociado" name="NPAsociado" placeholder="ejem: 48" type="number" class="form-control" min="1" max="10000" required>
<button type="submit" class="btn btn-primary" id="btnguardarstand">Registrar</button>
</form>
script
$(document).ready(function() {
$('form').submit(function(e) {
e.preventDefault();
// o return false;
});
});
$("#btnguardarastand").on("click", function() {
//alert("entro");
var formulario = document.getElementById('FormingreasarStand');
if (formulario.checkValidity()) {
Swal.fire({
title: 'esta seguro de registrar el Stand',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Si, deseo registrarlo',
cancelButtonText: 'Cancelar',
}).then((result) => {
if (result.isConfirmed) {
var datos = new FormData();
datos.append("accion", 'AddNewStand');
datos.append("nroStand", $("#iptnroStand").val());
datos.append("NPAsociado", $("#iptNPAsociado").val());
$.ajax({
url: "ajax/stands.ajax.php",
type: "POST", // error aqui puse method
data: datos,
processData: false,
contentType: false,
dataType: 'json',
success: function(respuesta) {
if (respuesta == "ok") {
Swal.fire({
icon: 'success',
title: 'registro ok',
showConfirmButton: false,
timer: 3000,
})
table.ajax.reload();
$("#iptnroStand").val("");
$("#iptNPAsociado").val("");
} else if (respuesta[2].indexOf("Duplicate") != -1) {
swal.fire({
icon: 'error',
title: 'El stand ' + $("#iptnroStand").val() +
' Ya esta registrado',
text: 'No puedes registrar un stand que ya esta regitrado en la base de datos!',
footer: respuesta,
showConfirmButton: false,
timer: 5000,
})
// aqui pongo el foco en el input dato incorrecto
$("#iptnroStand").focus();
} else if (respuesta[2].indexOf("null") != -1) {
swal.fire({
icon: 'error',
title: 'El Número de padron: ' + $("#iptNPAsociado")
.val() +
' no existe',
text: 'El Número de padron que ingresaste no esta registrado en la base de datos!',
footer: respuesta,
showConfirmButton: false,
timer: 3000,
})
// aqui pongo el foco en el input con dato incorrecto
$("#iptNPAsociado").focus();
} else {
swal.fire({
icon: 'error',
title: 'Erro al Registrar',
footer: respuesta,
showConfirmButton: false,
timer: 5000,
})
}
}
})
}
})
}
});
Pedro, try doing the
focus
when the alert closes. For example...