I have the following form:
<form id="frm-registrarusuario" action="../php/usuario.php" method="post">
<input name="usuario" id="usuario" type="text">
<input name="clave" id="clave" type="text">
<button type="submit" name="submit" >Registrar</button>
</form>
The user.php file:
if(isset($_POST["validarUsuario"])){
header('content-type: text/json');
$usuario = $_POST["usuario"];
$where = array("usuario"=>$usuario);
if($obj -> validarUsuario("usuario",$where))
{
$data["respuesta"] = "EXISTE";
$data["mensaje"] = "¡ERROR!, El usuario ingresado ya existe";
}else{
$data["respuesta"] = "NOEXISTE";
}
echo json_encode($data);
}
the jquery code:
$(document).ready(function () {
$("#frm-registrarusuario").on("submit",function (e) {
e.preventDefault();
var usuario = $("#usuario").val();
$.ajax({
method:'post',
url:'../php/usuario.php',
data:{
usuario:usuario,
validarUsuario:1
},
dataType:'json',
success:function (data) {
if(data["respuesta"]=="EXISTE"){
alert(data["mensaje"]);
return false;
}
if(data["respuesta"]=="NOEXISTE"){
return true;
}
}
});
});
});
What I intend to do is that if the user exists, the form does not allow sending data, however when the user exits, he still sends the form. It seems that the ajax runs at the end of everything, after sending the form. Any idea how to solve it?