Lincoln Binda Lincoln Binda - 4 months ago 9
SQL Question

Search in mysql by Javascript

Good morning , I'm trying to do a check if the cpf entered already exists in the database , I'm using laravel , someone could help me in this function please ?

In my controller I seek and I list all cpfs in 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'm not sure how to make a function in javascript to get 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 am beginner in the field and do not really know how to do this work, I just wanted the cpf existed already setasse one CustomValidity in the browser and not let him continue with the registration , if anyone can help me or give me other ways of how solve this I would be very grateful!

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

Answer

ok i will attempt to try and do what your looking for:

Say the html input is something like:

 <input type="text" value="" id="cpff" />

Then we have some js to get the details within the above input box, the below does this on each lette typed into the input so its constantly checking as the user types then giving a visual reposnse as the user types so they now if its taken or not as typing:

   $('input#cpff').on('keyup', function(evt){
      //-- get for value on keyup
      var inputValue = this.value;
      //-- call to the controller to check
      $.ajax({
        url : '/check/cpff/',
        type: 'POST',
        data {cpffValue : inputValue},
        success : function(res){
          console.log(res);
          if(res === 'succcess'){
            //-- output a tick icon to let the user know all is ok so far
          } else {
            //-- output an red x to sai no its been used
          }
        },
          error : function(msg, x){
            console.log(msg);
          }
      })

    }); 

So for each letter typed in the input box the code sends the value to the route /check/cpff and then this returns a boolean / string back to see if its ok or not, hopefully your still with me?

Route:

  Route::post('/check/cpff', ['uses' => 'ControllerName@checkCPFF']);

Controller

    Class ControllerName extends Controller {

        public function checkCPFF(Request $request){
            //-- get the ajax data variable -- cpffValue
            $formValue = $request->get('cpffValue');
            //-- lets chec to see if we have an entry with those values
            $doesItExsist = ModelName::where('field_name',$formValue);
            //-- if we have a result from the DB then return not allowed
            return ($doesItExsist) ? 'not-allowed' : 'allowed'
        }

    }

SO the above controller takes the data value that the form submits, then checks the required db table and does a search for that value, if it cannot then the user has a valid entry if there is a value found then the user must change their entry etc

its a bit crude and i have nothing to test this with etc so might / will need better working / writing to work as such, you can always change the js to do the same on submitting the form instead etc,

here is a link to test out the input section. to veiw the result open the console up so you can see the values being typed. Example of keyup

i hope it gives your a sort of starting base for help...