Bank check with ajax

0

I've never worked with ajax, but I need a form in a form to search the MySQL to check if the login the user is typing already exists.

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

Type I even down to a model on the net but it's incomplete what do 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 database, I know, I set the SQL calm, but how do I return an answer to app and if it is not positive type there is that login what I put on the side of javascript to signal that it exists?

    
asked by anonymous 26.09.2016 / 19:55

2 answers

1
___ erkimt ___ Bank check with ajax ______ qstntxt ___

I've never worked with ajax, but I need a form in a form to search the %code% to check if the login the user is typing already exists.

In the input field I put a %code% for when the guy jumps from field the system to verify if there is that login.

Type I even down to a model on the net but it's incomplete what do I do next?

$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 '';
}

What do I do now on the PHP side? How do I get this variable? Search the database, I know, I set the %code% calm, but how do I return an answer to app and if it is not positive type there is that login what I put on the side of %code% to signal that it exists?

    
______ ___ azszpr155060

Part of PHP

If user scored an array used to remove unwanted characters.

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

                    }
                }
            });
        }
    })
});

I did something of the genre in javascript see if it helps.

$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 '';
}
    
______ azszpr155056 ___

In the part of the code "success: function (data) {}", in the variable %code% you will get what you received from the response,

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

  

.php file

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

                    }
                }
            });
        }
    })
});
  

in AJAX

%pre%

*** Note that the new .php file in this case may contain HTML content and not just the message, where the variable date will be composed by the whole. Hope this helps.

    
___
26.09.2016 / 20:43
1

In the part of the code "success: function (data) {}", in the variable data you will get what you received from the response,

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

  

.php file

<?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 that the new .php file in this case may contain HTML content and not just the message, where the variable date will be composed by the whole. Hope this helps.

    
26.09.2016 / 20:38