0
votes

i wanna ask something. how i can update data from database from without submit.

for example :

<select name = 'city'>
<option value = '1' SELECTED>new york</option>
<option value = '2'>france</option>
<option value = '3'>indonesia</option>
</select>

if i change my select option into indonesia, my script will auto update into database set value city into '3', and indonesia will automatic be selected.

<select name = 'city'>
<option value = '1'>new york</option>
<option value = '2'>france</option>
<option value = '3' SELECTED>indonesia</option>
</select>

please help me, thank you

2

2 Answers

0
votes

To do that, you need to use Ajax, which will allow you to do server side call without refreshing the page.

on change event of dropdown you need to write the script which will update do server side processing.

e.g. http://www.w3schools.com/ajax/ajax_database.asp

Thanks Amit

0
votes

Html

<select name = 'city' data-url="{{url('/')}}" data-token="{{ csrf_token() }}>
<option value = '1' SELECTED>new york</option>
<option value = '2'>france</option>
<option value = '3'>indonesia</option>
</select>

Jquery

$('select').on('change',function(){
  var city_id =  $( "select option:selected" ).val();
  var token = $(this).data('token');
  var base_url = $(this).data('url');
     $.ajax({
        url:base_url+'/update_city',
        type: 'POST',
        data: { _token :token,city_id:city_id },
        success:function(msg){
           alert("success");
        }
     });

})

Route

Route::post('update_city','TestController@updateCity');

TestController.php

public function updateCity(Request $request){
  $place_id = $request->get('city_id');
  // do database operations required
  return 'success';

}