I make a small similar system. I already managed to make an ajax message in my database, where I increase tblarticles using row.like
with 1. Now on the same page where I am making an ajax message, I want to update the field value after each ajax message, Now my problem is that I need to update before I see 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]);
This is the last range that I want to change with the current data published after the ajax post.
source
share