Jalloh
Jalloh

Reputation: 67

Ajax update field database field in laravel blade template

I have a list of hotels in a select I want update the database base on the hotel selected if it update the field I want to alert it on the home page. this is the ajax function

function showRoom($hotel_plan_id) {
var id = $hotel_plan_id;
if (id !== "") {
  $.ajax({
    type: "POST",
    dataType: 'JSON',
    url:'{{ route('home', '') }}/'+id,
    data:{_token:'{{ csrf_token() }}'},
    success:function(data){
       alert(data);
    },
    error: function (result) {
    alert("Error occur in the update()");
    }
  });


}

} my controller

public function update(Request $request, $hotel_plan_id)
{ 
    $hotel=plan_hotel::where('hotel_plan_id', $hotel_plan_id)->first();
    $hotel->hotel_name = $request->input('hotel_name');
    //$hotel ->room_name = $request->input('room_name');
    $hotel->save();
   // return redirect('/home')->with('success', 'price updated');

}

my route

Route::post('home/{hotel_plan_id}', 'HomeController@update')->name('home');

my select form

 {!! Form::select('hotel_name', array($item[4], $item[10]),'S', array('style'=>' Border:none; ', 'id' => 'hotel_id', 'onclick'=>"showRoom(this.value, $item[8])"));!!}

Upvotes: 0

Views: 126

Answers (3)

Poldo
Poldo

Reputation: 1932

You have error with ajax url and you dont also pass the value of hotel name.

Check this changes.

function showRoom($hotel_plan_id) {
var id = $hotel_plan_id;
if (id !== "") {
  $.ajax({
    type: "POST",
    dataType: 'JSON',
    url:'/home/' + id,
   data:{_token:'{{ csrf_token() }}', hotel_name: 'Your value here'},
  });
}

Upvotes: 1

Siraj Ahmad
Siraj Ahmad

Reputation: 476

You have to return json object like this

return response->json([
       'status'    =>  200,
       'result'    => true,
       'error'     => false,
       'data'      => $hotel
 ]);

Upvotes: 0

Thai Nguyen Hung
Thai Nguyen Hung

Reputation: 1212

Please return json response

return response()->json();

Upvotes: 0

Related Questions