Search in mysql by Javascript

0

Good morning, I'm trying to make a check if the cpf typed already exists in the bank, I'm using laravel, could anyone help me in this function please?

In my controller, I search and ready all cpfs of the bank:

    $cpfduplicado = UsuarioEsic::lists('doc', 'id')->all();
foreach ($cpfduplicado as $cpf)
            if ($cpf == $request->cpf)
                $cpf = 'verdadeiro';
//                return redirect('/Esic/CadastroFisica');
return redirect('/Esic/Sucesso');

I do not know very well how to do a function in javascript to get this data and set a custom validity of the browser, I did something like this:

$(function cpfdupli(input) {
    $('.btnCadastroo').on('click', function (json){
        if (json.cpf === 'verdadeiro'){
            document.getElementById('cpff');
            input.setCustomValidity('Cpf já existe.');
        } else {
            input.setCustomValidity('');
        }
    });
});

I'm a beginner in the branch and I'm not really sure how to make it work, I just wanted it if cpf already existed, put a CustomValidity in the browser and do not let it continue with the registration, if someone can help me or give me other Ways to solve this I would be very grateful!

Note: Any questions about the code will be available to provide.

    
asked by anonymous 11.07.2016 / 15:41

1 answer

1

Friend, I did not understand why you should LIST to do this verification. You could do a query similar to this.

$cpfduplicado = UsuarioEsic::where('cpf', $request->cpf)->get();

So it only returns the CPF if it is duplicated.

Then you could do a check if the return is empty (if you do not find an equal cpf)

if(empty($cpfduplicado)){
  return redirect('/Esic/Sucesso');
}

If it is JSON , you can return a value as well. I hope I have helped

    
11.07.2016 / 16:34