Support the ongoing development of Laravel.io →
Database Views Blade

please help me. how i can update data from database using select option, ajax onchange without submit.

my view :

{!! Form::model($UserAccess,['method' => 'POST','url'=>['setting/updaterole']]) !!}
                    <table class="dataTable" id="table-user">
                        <thead class="grey lighten-3">
                            <tr>
                                <td class="center-align no-sort">Photo</td>
                                <td class="center-align">Fullname</td>
                                <td class="center-align">Rule</td>
                                <td class="center-align">Action</td>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $hitung = $UserAccess->count(); ?>
                            @foreach($UserAccess as $list)
                            <tr>
                                <td class="center-align" width="50">
                                    @if($list->avatar == NULL)
                                    <img class="circle responsive-img" width="50" src="{{asset(config('param.url_uploads').'blank.jpg')}}"/>
                                    @else
                                    <img class="circle responsive-img" width="50" src="{{$list->avatar}}"/>
                                    @endif
                                </td>
                                <td class="red1-text lato-bold center-align">{{$list->name}}</td>
                                <td class="center-align" width="150">
                                    <select id="selectrole" name="selectrole" data-url="{{url('/setting/updaterole')}}" data-token="{{ csrf_token() }}">
                                        @foreach($UserAccessRole as $listRole)
                                            <option value="{{$listRole->id}}" @if($list->user_access_role_id_fk == $listRole->id) selected="selected"@endif>{{$listRole->name}}</option>
                                        @endforeach
                                    </select>
                                </td>
                                <input type = "hidden" value = "{{$list->id}}" name = "idmain">
                                @if($hitung > 2)
                                    <td class="center-align">
                                        <a href = "remove_access/{{$list->id}}/delete" >Remove Access<a/>
                                    </td>
                                @else
                                    <td class="center-align">Remove Access</td>
                                @endif
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
{!! Form::close() !!}
@section('page-script')
<script type="text/javascript">
$('select').on('change',function(){
var city =  $( "select option:selected" ).val();
var token = $(this).data('token');
var base_url = $(this).data('url');
 $.ajax({
    url:base_url+'/updaterole',
    type: 'POST',
    data: { _token :token,city_id:city_id },
    success:function(msg){
       alert("success");
    }
 });


})
</script>
@endsection

my controller :

public function doUpdateAccessRole($id, Request $request)
{
   $place_id = $request->get('id');
   $Role = UserAccess::findOrFail($id);
   $Role->user_access_role_id_fk = $id_role;
   $Role->update($request->all());
   return 'success';
}

my routes

Route::post('setting/updaterole',['uses'=>'SettingController@doUpdateAccessRole','as'=>'updateaccessrole']);

i don't understand whats wrong with my code. when i change the select option value nothing happen with my program. please help me, thank you.

Last updated 3 years ago.
0

Is the form rendering as you expect?

Using your browser developer console, is the ajax call firing correctly?

Is the route being hit at all? Have you just echoed out something at the top of the method just to check?

I'd recommend returning some kind of JSON object instead of just a string.

0

nothing be happen. when i change my option, my program did not respond

0

Try and answer the questions, I'll be in a better position to help you.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

hartolio hartolio Joined 9 Feb 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.