lundi 7 août 2017

Laravel and ajax pass user id and role on select

I have a page like this:

@extends('admin.master') @section('title', 'Users')
@section('content')
<meta name="csrf-token" content="">
<div class="table-responsive">
<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Role</th>
            <th>Created At</th>
            <th>Updated At</th>
            <th>Roles</th>
            <th>Buttons</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($users as $user)
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td>
                <input type="hidden" name="id" id="id" value="">
                <select  class="required form-control form-field" name="type" onchange="role(this.value);" id="type">
                    <option selected disabled>Choose role...</option>
                    @foreach ($roles as $role)
                      <option value=></option>
                    @endforeach
                  </select>
                </td>
                <td>
                <i class="fa fa-pencil-square-o pull-left" style="color: yellow;" aria-hidden="true" data-toggle="modal" data-target="#updateUser"></i>
                <form method="POST" action=>
                
                
                <button type="submit" class="delete" style="border:none;"><i class="fa fa-trash-o pull-right" style="color:red;" aria-hidden="true"></i></button>
                </form>
                </td>
            </tr>
        @endforeach
    </tbody>
</table>
    <div class="fa add-button"><i class="fa fa-plus fa-2x" style="color: white;" aria-hidden="true" data-toggle="modal" data-target="#createUser"></i></div>
</div>
@include('admin.partials.user.updateUser')
@include('admin.partials.user.createUser')
@endsection


function role(role_value) {
 $.ajax({
        url: '/test2/' + role_value,
        method: 'POST',
        dataType: 'json',
        success: function(data) {
          console.log(data);
        },
        error: function(data) {
          console.log("wrong");
        },
        headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
        }
    });
}

For each user I have a row with data and dropdown with roles available. After I choose a role from dropdown I want to call a function which is ajax request that will pass selected role_id which works fine, however I also want to pass user_id from the same row as the specific dropdown I have choosen the value for. How can I do that?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire