Search in mysql by Javascript

Asked

Viewed 164 times

1

Good morning, I’m trying to make a check if the typed Cpf already exists in the bank, I’m using Portable, could someone help me in this function please?

In my controller I search and list all the database numbers:

    $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 don’t know very well how to do a function in javascript to take this data and set a custom browser validity, 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 business and I don’t really know how to make it work, I just wish that if Cpf already existed set a Customvalidity in the browser and didn’t let it continue with the registration, if anyone can help me or give me other ways to resolve this I would be very grateful !

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

  • like the browser customer does not have access to MySQL on your server, the JavaScript will not consult the MySQL (still good), then you will need to make a request using AJAX/XMLHttpRequest and return an object using the method json_encode.

  • But who queries mysql is my controller, I just need to play the controller information for javascript and then check if the typed Cpf is equal to any of the Cpf’s listed by the controller and then set a browser function.

  • The only link between your PHP controller and the client page is the HTTP requests (although spaghetti written by some PHP programmers gives the impression that PHP has some link with HTML and Javascript), so my comment remains.

  • Licoln, read the following link: How to change status with AJAX?, has a step by step how to make a request AJAX, the use of json_encode and how to read the return json and update the client page.

  • 1

    Man, thank you so much, I was at lunch, I’ll study it right now, thank you !

1 answer

1

Friend, I don’t understand why you make a LIST to do this check. You could do a query similar to this.

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

Thus only returns the CPF if it is duplicated.

After Voce could make 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, Voce can return a value as well. I hope I’ve helped

  • Helped me a lot friend, helped to improve my code, thank you very much, as I said, I am a beginner in the area, the way you spoke perfectly, however I need to inform the user that the informed Cpf already exists, That way, if it doesn’t go through the verification I can’t do a Customvalidity, which is only in javascript and is the only way I can warn the user and prevent him to follow the page without having to update or clear the fields already typed.... Anyway thank you very much, I continue studying and researching on the subject... : D

Browser other questions tagged

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