Validate if there is already a registration in the bank before filling out the form

0

I have the following form:

        <form class="form-horizontal" action="cadastrar.php" method="GET">
                <fieldset>
                    <legend>DADOS CADASTRAIS</legend>

                    <input type="hidden" required id="cep" name="cep" value="<?php echo $cep; ?>">

                    <div class="row">
                        <div class="col-md-3">
                            <label for="cpf">CPF</label>
                            <input type="text" 
                            class="form-control" 
                            required 
                            id="cpf" 
                            name="cpf" 
                            placeholder="SOMENTE NUMEROS">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <label for="nome">NOME</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required id="nome" 
                            name="nome"  
                            placeholder="NOME DO CLIENTE" 
                            required>
                        </div>
                    </div>       
                    <div class="row">
                        <div class="col-md-4">
                            <label for="end" >ENDERECO</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required 
                            id="end" 
                            name="end" 
                            placeholder="DIGITE ENDERECO">
                        </div>
                        <div class="col-md-2">
                            <label for="num">NUMERO</label>
                            <input type="text" 
                            class="form-control" 
                            required 
                            id="num" 
                            name="num" 
                            placeholder="NUMERO">
                        </div>
                        <div class="col-md-2">
                            <label for="comp">COMPLEMENTO</label>
                            <input type="text" 
                            class="form-control" 
                            id="comp" 
                            name="comp" 
                            placeholder="COMPLEMENTO">
                        </div>
                        <div class="col-md-4">
                            <label for="bairro">BAIRRO</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required 
                            id="bairro" 
                            name="bairro" 
                            placeholder="BAIRRO DO CLIENTE">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <label for="cidade">CIDADE</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required 
                            id="cidade" 
                            name="cidade" 
                            placeholder="CIDADE DO CLIENTE">
                        </div>   
                        <div class="col-md-6">
                            <label for="telefone">TELEFONE</label>
                            <input type="text" 
                            class="form-control" 
                            required 
                            id="telefone" 
                            name="telefone" 
                            placeholder="TELEFONE">
                        </div>            
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <label for="rg">RG</label>
                            <input type="text" 
                            class="form-control" 
                            required
                            id="rg" 
                            name="rg" 
                            placeholder="SOMENTE NUMEROS">
                        </div>
                        <div class="col-md-6">
                            <label for="email">E-MAIL</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required 
                            id="email" n
                            ame="email" 
                            placeholder="EMAIL DO CLIENTE">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <br>
                            <button type="submit" class="btn btn-primary">CADASTRAR</button>
                        </div>
                    </div>
                </fieldset>
                <!--FINAL FORMULARIO DE CADASTRO-->
            </form>

I have a functionCPF.php that returns the following json

http://localhost:8080/sys/functions/functionCPF.php?cpf=11122233344
  

If cpf exists:

 {"dados":{"cpf":"11122233344"}}
  

If it does not exist:

{"dados":{"cpf":0}}

Based on what I need:

If there is cpf: Alert "CPF ALREADY REGISTERED";

If no: Nothing happens and the person can continue registering

I thought of something with ajax or javascript but I did not find anything like this, could someone help me?

I was able to:

$(document).keydown(function(){
    var cpf = $("#cpf"); 
        cpf.blur(function() { 
            $.ajax({ 
                url: '../functions/functionCPF.php', 
                type: 'GET', 
                data:{"cpf" : cpf.val()}, 
                success: function(data) { 
                console.log(data); 
                data = $.parseJSON(data); 
                $("#resposta").text(data.cpf);
            } 
        }); 
    }); 


    });

PHP:

<?php
//database configuration
    $dbHost     = '127.0.0.1';
    $dbUsername = 'root';
    $dbPassword = '';
    $dbName     = 'master';
#Verifica se tem um cpf para pesquisa
if(isset($_GET['cpf'])){ 
    #Recebe o cpf Postado
    $VarCPF = $_GET['cpf'];
    #Conecta banco de dados 
    $Mysqli = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
    $sql = mysqli_query($Mysqli, "SELECT 
    cli_cpf 
        FROM 
         big_clientes 
         WHERE 
             cli_cpf = '".$VarCPF."'") or print mysql_error();
    #Se o retorno for maior do que zero, diz que já existe um.
    if(mysqli_num_rows($sql)>0) 
        echo json_encode(array('cpf' => 'CPF JÁ CADASTRADO!')); 
    else 
        echo json_encode(array('cpf' => 'CPF OK!' )); 
}

What I need now, if given CPF ALREADY REGISTERED - > Do not fill out the form.

    
asked by anonymous 11.04.2017 / 22:47

1 answer

0

Solution:

  

SCRIPT:

$(document).keydown(function(){
    var cpf = $("#cpf"); 
        cpf.blur(function() { 
            $.ajax({ 
                url: '../functions/functionCPF.php', 
                type: 'GET', 
                data:{"cpf" : cpf.val()}, 
                success: function(data) { 
                console.log(data); 
                data = $.parseJSON(data); 
                if(data.cpf) {

                    document.getElementById("myForm").reset();
                    $("#resposta").text(data.msg);


                 }
            } 
        }); 
    }); 
  

FORM

            <form id="myForm" class="form-horizontal" action="cadastrar.php" method="GET">
                <fieldset>
                    <legend>DADOS CADASTRAIS</legend>

                    <div class="row">
                        <div class="col-md-3">
                            <label for="cpf">CPF</label>
                            <input type="text" 
                            class="form-control" 
                            required 
                            id="cpf" 
                            name="cpf" 
                            placeholder="SOMENTE NUMEROS">
                        </div>
                        <div class="col-md-9">
                            <h3>
                                <span id='resposta' class="label label-danger">
                                </span>
                            </h3>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <label for="nome">NOME</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required id="nome" 
                            name="nome"  
                            placeholder="NOME DO CLIENTE" 
                            required>
                        </div>
                    </div>       
                    <div class="row">
                        <div class="col-md-4">
                            <label for="end" >ENDERECO</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required 
                            id="end" 
                            name="end" 
                            placeholder="DIGITE ENDERECO">
                        </div>
                        <div class="col-md-2">
                            <label for="num">NUMERO</label>
                            <input type="text" 
                            class="form-control" 
                            required 
                            id="num" 
                            name="num" 
                            placeholder="NUMERO">
                        </div>
                        <div class="col-md-2">
                            <label for="comp">COMPLEMENTO</label>
                            <input type="text" 
                            class="form-control" 
                            id="comp" 
                            name="comp" 
                            placeholder="COMPLEMENTO">
                        </div>
                        <div class="col-md-4">
                            <label for="bairro">BAIRRO</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required 
                            id="bairro" 
                            name="bairro" 
                            placeholder="BAIRRO DO CLIENTE">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <label for="cidade">CIDADE</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required 
                            id="cidade" 
                            name="cidade" 
                            placeholder="CIDADE DO CLIENTE">
                        </div>   
                        <div class="col-md-3">
                            <label for="cep">CEP</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required 
                            id="cep" 
                            name="cep" 
                            placeholder="CEP DO CLIENTE">
                        </div>   
                        <div class="col-md-3">
                            <label for="telefone">TELEFONE</label>
                            <input type="text" 
                            class="form-control" 
                            required 
                            id="telefone" 
                            name="telefone" 
                            placeholder="TELEFONE">
                        </div>            
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <label for="rg">RG</label>
                            <input type="text" 
                            class="form-control" 
                            required
                            id="rg" 
                            name="rg" 
                            placeholder="SOMENTE NUMEROS">
                        </div>
                        <div class="col-md-6">
                            <label for="email">E-MAIL</label>
                            <input type="text" 
                            style="text-transform:uppercase" 
                            class="form-control" 
                            required 
                            id="email" n
                            ame="email" 
                            placeholder="EMAIL DO CLIENTE">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <br>
                            <button type="submit" class="btn btn-primary">CADASTRAR</button>
                            <button type="reset" class="btn btn-default">CANCELAR</button>
                        </div>
                    </div>
                </fieldset>
                <!--FINAL FORMULARIO DE CADASTRO-->
            </form>
  

PHP

<?php
//database configuration
    $dbHost     = '127.0.0.1';
    $dbUsername = 'root';
    $dbPassword = '';
    $dbName     = 'master';

#Verifica se tem um cpf para pesquisa
if(isset($_GET['cpf'])){ 

    #Recebe o cpf Postado
    $VarCPF = $_GET['cpf'];

    #Conecta banco de dados 
    $Mysqli = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
    $sql = mysqli_query($Mysqli, "SELECT 
    cli_cpf 
        FROM 
         big_clientes 
         WHERE 
             cli_cpf = '".$VarCPF."'") or print mysql_error();

    #Se o retorno for maior do que zero, diz que já existe um.
    if(mysqli_num_rows($sql)>0) 

        echo json_encode(array('cpf' => TRUE,'msg' => 'CPF JÁ CADASTRADO!')); 

    else 
        echo json_encode(array('cpf' => FALSE ));


}
    
12.04.2017 / 00:33