AimForNuts
AimForNuts

Reputation: 91

How to populate view with database information after AJAX request

Hi there once again SO community. I've been developing a site and so far it's going pretty well. But today after a long day searching for a solution I can't understand nor find what the right path is...

I want to click on a button and a profile page where you can edit the fields appear. I can redirect to the page I want but I don't know how to send the user data so I can populate the fields.

Here is my button code on my view

<button class="btn btn-xs btn-warning dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false" style="border-color: black" id="dados_{{ $user->username }}"> Alterar Dados Pessoais

<i class="glyphicon glyphicon-cog"></i>

</button>

Here is the button AJAX request handler

if((this.id).indexOf("dados") != -1){

    var content = this.id.replace("dados_", "");

    $.get('callPermissions', {usernameSend:content, '_token': $('meta[name=csrf-token]').attr('content'),}, function(data){

    window.location.replace('settings');

});

And here is my callPermission Controller

public function callPermissions(Request $request)
{

  if($request->ajax()){

    $usernames = Input::get('usernameSend');

    if(isset($usernames)){

      $user = User::Where('username', '=', $usernames)->first();

      $returnHTML =  view('userOptions.settings')->render();

      return view('userOptions.settings');

    }else{

      Log::warning("Username não existe na base de dados.");

    }

  }

}

and here my Settings Controller

public function settings(Request $request)
{

    return view('userOptions.settings');

}

And here is the route

Route::get('/callPermissions', 'SidebarController@callPermissions');

I know the controller is wrong and from what I've read I should verify if the AJAX is successful and if it is handle i on the AJAX request. But from what I've understand I'm not using the Controller at all (even though it goes there). How should I send the user information (in this case the username, then I can get everything from the database) and then send it to the view? I've been searching and trying out stuff that doesn't work...since the return view("your_view") on the Controller doesn't work.

Sorry if I've been confusing and if you need additional information feel free to ask!

Thanks for your help!!

Edit: If I return this on the controller

return view('userOptions.settings', compact('user'));

and do a replace with the Ajax request as show above and add this to the settings view

<p> {{ $user->name }} </p>

I get the following error Undefined variable: user (View: C:\wamp64\www\siteXL\ideiasxl\resources\views\userOptions\settings.blade.php)

Is there anyway to send the parameters with a compact alike or I need to send it through the link? Was avoiding to show the username on the url.

Edit2: For further clarification, this works as intended

<button onclick="window.location='{{url('/settings/' . $user->username)}}'" type="button" id="dadosPessoais" class="btn btn-default">Alterar Dados Pessoais

<i class="glyphicon glyphicon-wrench"></i>

</button> 

but I was trying not to send id's and usernames through the URL.

If this is not achievable it's ok, but if there's a way I can't find it, that's why I'm asking

Upvotes: 1

Views: 85

Answers (1)

Laerte
Laerte

Reputation: 7083

I think you have to add a parameter in the Route and receive the data in the controller function. I'd do something like this:

Route:

Route::get('/callPermissions/{user}', 'SidebarController@callPermissions');

Controller:

public function callPermissions(Request $request, $user)
{
//get data related to $user
}

Ajax call:

$.get('callPermissions/'+userIdVariable, {usernameSend:content, '_token': $('meta[name=csrf-token]').attr('content'),}, function(data){

    window.location.replace('settings');

});

This would send the user id through the route.

To get the user id with JavaScript, you can make a hidden field in the Blade file and set the user id as the value. For example, if you using Form helper:

{{ Form::hidden('user_id', $user->id, array('id' => 'js-user-id')) }}

And then, in the JavaScript, you can get the value using something like this:

var userIdVariable = $('#js-user-id')->val();

Upvotes: 2

Related Questions