I am building an application where users can upload projects. I am implementing a system where users can 'Like/Unlike' other projects. I am trying to use an AJAX call to save likes. Users are able to like projects on the detail page of a project (/projects/{id})
I have a table users, projects and likes. My plan is to save the likes in the likes table obviously so a record looks like this: id, user_id, project_id. In the future I can do a COUNT query and find out how many likes each project has, etc.
For some reason when I click on the like button my application gets stuck and keeps loading until it crashes.
My files: Ajax call
$(document).ready(function(){
console.log("js loaded");
$('#likeform').on('submit', function(e) {
console.log("like submitted");
e.preventDefault();
$.ajax({
url: $(this).parent().attr('action'),
type: "post",
data: {'_token': token, 'user_id': $('input[name=user_id]').val(), 'project_id': $('input[name=project_id]').val()},
success: function(data){
alert(data);
}
})
});
});
My Form
{!! Form::open(array('url'=>'projects/'.$project->id.'/like','method'=>'POST', 'id'=>'likeform')) !!}
<input type="hidden" id="token" value="{{ csrf_token() }}">
{!! Form::Submit('Like', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
My Routes:
Route::get('/', 'LikeController@index');
Route::post('projects/{id}/like', 'LikeController@like');
My like function in the LikeController:
public function like()
{
if(Request::ajax()) {
$data = Input::all();
print_r($data);die;
}
}
action
attribute from your form while it doesn't have one. You did set anurl
instead. I'm not confident this is what you're looking for, but it's worth a try imo. Side note: building a form with theForm
facade will automatically set an hiddentoken
field, so there's no actually need to add to your form a second one. – MisterPaction
vsurl
was a nonsense. My bad. Still is quite strange you are not getting any error, at least within the server log. – MisterP