Bank check with ajax

Asked

Viewed 478 times

0

I never worked with ajax but I need in a form do a search on MySQL to check if the login that the user is typing already exists.

In the input field I put a onBlur for when the guy jumps from the field the system check if there is that login.

Like I’ve got a model on the net but it’s incomplete what I do next?

function TestaLogin(){

            $.ajax({ 
                url: 'VerificaDados.php', 
                type: 'POST',
                dataType:'json',                
                data: {"Cpf" : $("#cpf").val()}, 
                success: function(data) { 

                /*** Não faz nada pois está ok*/
            } 


        }); 

}

What do I do now on the PHP side? How do I get this variable? Search the bank I know, mount the SQL quiet, but how I return an answer to app and if it is not positive like there is that login what I put next to the javascript to signal that there is?

  • 3

    Do as you normally do, $_POST['cpf'], to return just one echo 'sucesso ou falha'; or you can send a json.

  • add an error: Function(data) { Alert(data); }. You have to check two things: If the name and path of the.php file is correct and it is actually returning a JSON.

2 answers

1

In the code part "Success: Function(data){}", in the variable data you will get what you received from reply, ie URL response call.

In a personal opinion, you can make a new file. php that checks if the login exists, getting the login that will be verified by POST and then write whether it already exists or not. This content you put in the new file . php is what you will receive in the variable data. After that you can make a field on your page that receives this content, example:

file . php

<?php
     $login = $_POST['loginParaVerificar'];
     $loginJaExiste = verificaLoginExistente($login);
     if($loginJaExiste){
          echo "Este login já existe no sistema";
     }else{
          echo "Este login NÃO existe no sistema";
     }
?>

in AJAX

success: fucntion(data){
        $('#divResposta').html(data);
        alert('AJAX retornado com sucesso!');
    }

*** Note, the new file . php in this case may contain HTML content and not only the message, where the variable date will be composed by the whole. I hope I’ve helped.

1


Part of PHP

if user marks I used an array to remove unwanted characters.

$retira = array('.','-','(',')',' ','/','_');
$cpf = isset($_REQUEST["cpf"]) ? trim(str_replace($retira, '', $_REQUEST["cpf"])) : null;

//consulta no banco de dados
$result = $_container['cadastroFornecedores']->searchCpf($cpf);

//se retornar é porque existe já e voce retorna ele próprio
//caso contrário retorna vazio, ou o que for necessário
if(sizeof($result) == 0){
   echo $cpf;
}else{
   echo '';
}

made something similar in javascript see if it helps you.

$(document).ready(function() {

   $('#usuario_cpf').blur( function(){

        if ($('#usuario_cpf').val() != ''){

            var dados = $('#usuario_cpf').val();

            $.ajax({
                method: 'POST',
                url: 'search-cpf.php',
                data: {
                    cpf: dados
                },
                beforeSend: function(){
                     $('.load-cpf').css({
                         display: 'block'
                     });
                },
                success: function(data){
                    $('#usuario_cpf').val(data);
                    if($('#usuario_cpf').val() == ''){
                        setTimeout(function(){
                            $('#usuario_cpf').val(data);
                            $('.load-cpf').css({
                                display: 'none'
                            });
                            $('.error-cpf').css('display', 'block').fadeOut(2000);
                        }, 500)
                    }else{
                        setTimeout(function(){
                            $('#usuario_cpf').val(data);

                            $('.load-cpf').css({
                                display: 'none'
                            });
                        }, 500);

                    }
                }
            });
        }
    })
});
  • Pow gave right this second option! I have no way to thank... Thank you very much! The first I believe is right tbm Valew to both! : D

Browser other questions tagged

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