0

This is my function where I need to make a response in ajax

function load_id_user(id) {
    var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
    alert(id);

    $.ajax({
        url: 'load-user',
        type: 'POST',
        data: {_token: CSRF_TOKEN,id:id},
        success: function (data) {
           $( "#user_fields" ).html( data );
        }
    });
}

this is my route.

Route::post('load-user','UserController@load_id');

and this is my controller

public function load_id() {
    $var=DB::select('select * from user where user_id="$_POST[id]"');

    //$var=Usuarios::select()->where("user_id","=",$_POST['id'])->get();
    foreach ($var as $user) {
        echo '<input type="text" name="cedula" value="'.$user->user_id.'" onblur="load_id_user(this.value);" placeholder="Cedula" class="form-control" required>';
    }
}

when I try to run this the console say error 500, try everything and nothing works.

Mhurtado
  • 13
  • 5

1 Answers1

0

Try changing your controller code to

public function load_id (Request $request) {

    $id = $request->input('id');
    $response = '';

    $var = Usuarios::where("user_id", "=", $id)->get();

    foreach ($var as $user) {
        $response .= '<input type="text" name="cedula" value="'.$user->user_id.'" onblur="load_id_user(this.value);" placeholder="Cedula" class="form-control" required>';
    }

    return $response;
}

and your ajax request to

$.ajax({
    url: window.location.origin + '/load-user',
    type: 'POST',
    data: {
        _token: CSRF_TOKEN,
        id:id
    },
    success: function (data) {
        $( "#user_fields" ).html( data );
    }
});
linktoahref
  • 7,812
  • 3
  • 29
  • 51