Reputation:
The issue i am facing is that when i click on active button all the article's active button change to inactive.. i want to change the status of that article id which i set to inactive from active. All articles will remain active or inactive as described status in database.how i can get this? If i Inactive Article button changes to Inactive of that article which is clicked not all articles and when i refresh page all articles status set to Active but in database their status changed to inactive.
Html:
<button class="btn btn-sm btn-danger delete-article-btn"
onclick="deleteArticle({{$article['aID']}})">Active
</button>
Script:
<script>
function deleteArticle(id) {
$.ajax({
url: '{!! URL('/pay/dash/panel/delete_article') !!}',
type: 'post',
dataType: 'json',
data: {'user_id': id},
success: function (response) {
console.log(response);
console.log('Article ID',id);
if (response == 1) {
$('.delete-article-btn').html('Inactive');
} else {
$('.delete-article-btn').html('Active');
}
}, error: function (error) {
}
});
}
</script>
Controller:
public function deleteArticles(Request $request)
{
$userId = $request->input('user_id');
$result = 0;
$checkArticle = Article::where('id', $userId)->where('is_deleted',0)->get();
if (count($checkArticle)>0) {
Article::where('id', $userId)->update(['is_deleted' => 1]);
$result = 1;
} else {
Article::where('id', $userId)->update(['is_deleted' => 0]);
$result = 0;
}
return $result;
}
Upvotes: 0
Views: 3661
Reputation: 3328
<button class="btn btn-sm btn-danger delete-article-btn mybtn-{{$article['aID']}}"
rel="{{ $article['aID'] }}" rel2="1">Active
</button>
<script>
$(document).on('click','.delete-article-btn',function(){
var id = $(this).attr('rel');
var status = $(this).attr('rel2');
$.ajax({
url: '{!! URL('/pay/dash/panel/delete_article') !!}',
type: 'post',
dataType: 'json',
data: {'user_id': id,'status':status},
success: function (response) {
console.log(response);
console.log('Article ID',id);
if (response == 1) {
$('.mybtn-'+id).html('Inactive');
$('.mybtn-'+id).attr('rel2',0);
} else {
$('.mybtn-'+id).html('Active');
$('.mybtn-'+id).attr('rel2',1);
}
}, error: function (error) {
}
});
})
</script>
public function deleteArticles(Request $request)
{
$userId = $request->input('user_id');
$status = $request->input('status');
$result = 0;
if ($status==0) {
Article::where('id', $userId)->update(['is_deleted' => 1]);
$result = 1;
} else {
Article::where('id', $userId)->update(['is_deleted' => 0]);
$result = 0;
}
return $result;
}
Upvotes: 2
Reputation: 483
You are changing the the html content of all the buttons with the class ".delete-article-btn"
if (response == 1) {
$('.delete-article-btn').html('Inactive');
} else {
$('.delete-article-btn').html('Active');
}
You have to specify which button you have to update maybe with a data attribute, as you prefer..
if (response == 1) {
$('.delete-article-btn[data-id=X]').html('Inactive');
} else {
$('.delete-article-btn[data-id=X]').html('Active');
}
Also, server side it seems you are targetting ALL articles from an user, i think you have to get only the Article you want to disable or not.
Upvotes: 0