vendredi 10 février 2017

Using Ajax to implement controller method which is passed an ID

I have a button that toggles a boolean value in my DB table. It works but reloads the page each time, so I'd like to toggle it using AJAX. I'm not sure how to do an AJAX post request that can provide the controller with the ID it needs. This is what I have so far:

TaskController Method:

    public function updateCompleted($id)
{

    $task = Task::findOrFail($id);

    if($task->completed) {
        $task->completed = false;
    } else {
        $task->completed = true;
    }

    $task->save();

    Session::flash('message', 'Task updated!');
    return redirect('tasks');
}

Button used to toggle (in this case, the first task with ID of 1):


    

    @if ($task->completed)
        
    @else
        
    @endif



AJAX that doesn't work...

    $('.ajaxSubmit').click(function(e) {
    e.preventDefault();
        $.ajax({
            url: 'tasks/complete/{id}',
            type: 'POST',
            success: function(response)
            {
                console.log("working");
            }
        });
   });

And route:

Route::patch('tasks/complete/{id}', 'TaskController@updateCompleted');

How do I get the Ajax to work? Thanks



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kQLqLp
via IFTTT

Aucun commentaire:

Enregistrer un commentaire