0

I have a laravel page, i am sending mobile number from my javascript in my view to validate it. I have routed it properly still I am getting this error. I am posting my code snippets below, please check and help me if any errors.

profile.blade.php

function sendotp()
    {
        var countryData = $("#country_selector").countrySelect("getSelectedCountryData");
        var isd = countryData.isd;
        $("#isdcode").val(isd);
        var isdcode = $("#isdcode").val();
        var mobile=$("#telephone").val();
        var email=$('#Email').val();
        $.ajax({
          url:'{{url("ajax/checkphone")}}',
          method:'post',
          data:{'mobile':mobile},
          success:function(output)
          {
            obj = JSON.parse(data);
            if(obj.status=='true')
            {
              alert("Number already exists.");
              window.location ="{{url('profile')}}";
            }
            else {

              $.ajax({
                  url:'{{url("ajax/registerotp")}}',
                  method:'post',
                  data:{'isdcode':isdcode,'phone':mobile,'reg_email':email, 'type': 'Update'},
                  success:function(output)
                  {
                      obj = JSON.parse(output);
                      if(obj.status=='1')
                      {
                          $("#modal-otp").modal({
                              backdrop: 'static',
                              keyboard: false
                          });
                          linkactivate();
                      }
                      else
                      {
                          $("#otp_msg").html('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>'+obj.sms+'</div>')
                      }
                  }
              });

            }
          }
        });
    }

web.php

Route::post('ajax/checkphone', 'AjaxController@checkphone');

AjaxController.php

function checkphone($mobile_no){
    $numbers = Users::all()->filter(function ($record) use ($mobile_no){
        if(owndecrypt($record->mobile_no) == $mobile_no){
            $data['message']="true";
        }
        else{
            $data['message']="false";
        }
    });
    echo json_encode($data);
}

This is the error which I get.

enter image description here

Thanks, help appreciated.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Raj Shah
  • 151
  • 1
  • 14

2 Answers2

1

Adding $data = [], define data type before use to avoid 500 error

function checkphone($mobile_no){
    $data = [];// define data type here
    $numbers = Users::all()->filter(function ($record) use ($mobile_no){
        if(owndecrypt($record->mobile_no) == $mobile_no){
            $data['message']="true";
        }
        else{
            $data['message']="false";
        }
    });
    echo json_encode($data); 
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
siddhant sankhe
  • 623
  • 6
  • 12
0

You receive incorrect data

function checkphone(Request $request){
    $postData = $request->all();
    $numbers = Users::all()->filter(function ($record) use ($postData){
        if(owndecrypt($record->mobile_no) == $postData['phone'){
            $data['message']="true";
        }
        else{
            $data['message']="false";
        }
    });
    echo json_encode($data);
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95