Reputation: 205
I am making a small like system. I already succeeded to make an ajax post to my database, where I increase the tblarticles with row.like
with 1. Now on the same page where I am making the ajax post, I want to update the value of the field after every ajax post. Now my problem is that i need to refresh, before seeing 1 instead of the standard 0
Ajax post
$.ajax({
url:"articles" ,
type: "post",
data: {
'user_id':userid[0].value,
'article_id':articleid[0].value,
'user_id_fk':useridarticle[0].value,
'_token': token[0].value
},
success: function(data){
console.log(data);
}
});
view article/index.blade.php
@foreach($articles as $article)
<div class="work-container">
<article class="work-item">
<a href="{{action('HomeController@showArticle', [$article->id, $article->title])}}" class="work-thumbnail">
<img src="/articlePics/{{$article->image}}" alt="work-item">
</a>
<div class="work-info">
<span class="user">{{$article->title}}</span>
<div class="actions">
<form id="actionForm" action="" method="post" role="form">
<input type="hidden" name="articleId" value="{{$article->id}}">
<input type="hidden" name="userIdFk" value="{{$article->user_id}}">
<input type="hidden" name="userID" value="{{Auth::id()}}">
<input type="submit" name="like" class="likeBtn" id="test" value="Like">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
</form>
<span class="likes" name="currentLikes">{{$article->likes}}</span>
</div>
</div>
</article>
</div>
@endforeach
Controller
if(Request::ajax()) {
$data = Input::all();
$user_id = Auth::id();
$article_id = Input::get('article_id');
$user_id_fk = Input::get('user_id_fk');
$results = DB::select( DB::raw("SELECT * FROM likes WHERE user_id = '$user_id' AND article_id = '$article_id'") );
$likedOrNot = DB::table('likes')
->where('article_id', $article_id)
->where('user_id', $user_id)
->pluck('likes');
$currentLikesDB = DB::table('articles')->where('id', $article_id)->pluck('likes');
echo json_encode(['likesnumber' => $currentLikesDB]);
It is the last span I want to change with the current data posted after the ajax post.
Upvotes: 5
Views: 3831
Reputation: 8350
try:
in the articles
echo the like number with json_encode
.
$currentLikesDB = DB::table('likes')->where('article_id', $article_id)->first();
$likesnumber = $currentLikesDB->likes;
return json_encode(['likesnumber' => $likesnumber]);
then ajax:
$.ajax({
url:"articles" ,
type: "post",
data: {
'user_id':userid[0].value,
'article_id':articleid[0].value,
'user_id_fk':useridarticle[0].value,
'_token': token[0].value
},
success: function(data){
$('.likes').html(data.likesnumber);
}
});
Upvotes: 2
Reputation: 4246
in your ajax request, return the total counts as a JSON and in you ajax's success function write this
success: function(data){
$('.likes').html(data.likesCount);
}
Upvotes: 0
Reputation: 11987
In your controller, after doing requires task, fire a query to get the counts.
Echo then in json_encode()
.
In ajax success()
, you can use parse the data and add where ever you want.
If you want to fetch only one value, no need of json_encode()
.
Directly echo the value and use in ajax success()
Upvotes: 0