2

I am using Laravel 5.4 and I have Check Out button in my table. I want when I click that button, it will update check out time in the database and update my table. I am trying to use ajax, but it is not working, but sometimes it is working too but not reload the table so I need to refresh the page manualy.
Here is my button code:
<a type="button" name="checkout_btn" id="checkout_btn" data-id=" {{ $Data->VST_ID }}" class="btn btn-primary">Check Out</a>

This is my ajax code:

$('#checkout_btn').click(function addseries(e){
   var VST_ID = $(e.currentTarget).attr('data-id');
   alert("dfads");
   $.ajax({
      type: "GET",
     url: 'visitor/checkout',
     data: "VST_ID="+VST_ID,
     success: function(data) {
     console.log(data);

   }
}); 
}); 

Here is my controller code:

public function Checkout(Request $request)
{   
    $visitor = Visitor::where("VST_ID",$request['VST_ID'])->first(); 
    $visitor->VST_CHECKOUT = date('Y-m-d H:i:s');
    $visitor->UPDATED_AT = date('Y-m-d H:i:s');
    $visitor->UPDATED_BY = 'User';
    $visitor->save();
    return redirect()->route('Visitor.VList')->with('message','The Visitor has been Checked Out !');       
}
1
  • You javascript wont update the table on it's own - the table doesn't know that there is now new data in the database. You need to return the new data in your checkout function and manually update the table rows Commented Apr 13, 2018 at 3:50

2 Answers 2

1

You can do this by two methods.

1st: by making different view for table body.

In controller set view as below Example

 if($request->ajax())
 {
    $records=\View::make('admin.settings.state.state-holiday-tbody',['contents'=>$contents,'active'=>$active,'tab'=>$tab])->render();
    return response()->json(array('html'=>$records));
 }

and then perform for loop to append data in table body.

Note: call function to get all table records on document ready.

2nd: Make table body using javascript for loop by getting all records in response.

Sign up to request clarification or add additional context in comments.

Comments

1

I think if you want to update the data, the method is PUT and why are you using GET method?

$('#checkout_btn').click(function addseries(e){
var VST_ID = $(e.currentTarget).attr('data-id');
   $.ajax({
     type: "PUT",
     url: 'visitor/checkout' + VST_ID,
     success: function(data) {
       console.log(data);
   }
}); 
}); 

change your controller

public function Checkout(Request $request, $id)
{   
    $visitor = Visitor::find($id); 
    $visitor->VST_CHECKOUT = date('Y-m-d H:i:s');
    $visitor->UPDATED_AT = date('Y-m-d H:i:s');
    $visitor->UPDATED_BY = 'User';
    $visitor->save();
    return redirect()->route('Visitor.VList')->with('message','The Visitor has been Checked Out !');       
}

and change your route.php

Route::put('visitor/checkout/{id}', 'visitorController@Checkout');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.