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

Asked

Viewed 1,165 times

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 which returns the following json

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

If there is a Cpf:

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

If there is no:

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

Based on that what I need:

If there is a Cpf : Alert "CPF ALREADY REGISTERED";

If not: Nothing happens and the person can continue to register

I thought of something with ajax or javascript but I couldn’t find anything like this, someone could help me?

I made it 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 you give CPF ALREADY REGISTERED --> Do not let fill in the form.

  • Check out the jquery ajax! http://api.jquery.com/jquery.ajax/, and use the onblur event (https://www.w3schools.com/jsref/event_onblur.asp) in #Cpf

  • @Robertocorreia I implemented with your tip, but I stopped at the last detail, can you see if you can help me? I added the question.

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 ));


}

Browser other questions tagged

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