Validate Cpf with ajax puchando from mysql

Asked

Viewed 201 times

0

Guys I’m trying to validate a Cpf with ajax I look for Cpf in the bank to see if it already exists it gives an alert and does not submit the form if there is no it has to submit, but in my case it is submitting follows the code

html:

<div class="container">
        <section>

            <h4 class="pb-2 mt-3">Novo Paciente</h4>
            <form action="paciente-action-inserir.php" method="post" onsubmit="return teste()" class="form-horizontal" id="form">
                <div class="form-group">
                    <label class="control-label">CPF</label>
                    <input type="text" name="cpf"  class="form-control" required="" placeholder="Digite o cpf aqui" id="cpf"  required="">
                </div>
                <div class="form-group">
                    <label class="control-label">Nome</label>
                    <input type="text" name="nome"  class="form-control" required="" placeholder="Digite o nome aqui" id="nome" required="" >
                </div>
                <div class="form-group">
                    <label class="control-label">Telefone</label>
                    <input type="tel" name="telefone"  class="form-control" required="" placeholder="Digite o telefone aqui" id="telefone" required="">
                </div>
                <div class="form-group">
                    <label class="control-label">E-mail</label>
                    <input type="email" name="email"  class="form-control" required="" placeholder="Digite o email aqui" id="email" required="">
                </div>
                <button type="submit" class="btn btn-primary" id="submit">Cadastrar</button>

            </form>

js:

function teste(){
    let pegar = $("#cpf").val();
    let cpf = pegar.replace(/[.-]/g,"");
    $.ajax({
        method: "POST",
        url: "verificar.php",
        dataType:"json",
        data:{"cpf": cpf} ,
        success:function(retorno){


            if(retorno['resultado'] != null){
                alert("cpf ja existe");
                return false;
            }
            else{
                return true;
            }

        },
        error:function(){

        }

    })

}

php:

<?php 
require_once "banco-paciente.php";

$cpf = $_POST['cpf'];

$sql = "SELECT cpf FROM pacientes WHERE cpf = '$cpf'";
$query = mysqli_query($conexao,$sql);
$result = mysqli_fetch_assoc($query);
$retorno = array("cpf"=>$cpf, "resultado"=>$result);

echo json_encode($retorno);




?>
  • Missing PHP code. Also put table structure if possible

  • Adriano, I can’t guarantee that this is the case, but I believe that when the query in the database does not find any results, it returns an empty array, no null, then instead of making the comparison if (retorno.resultado != null), you should do something like if (retorno.resultado.length > 0)

  • did the same thing

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.