I am trying to update the value (views) of a table row using Ajax when an onclick event is called.
The views are updated but the problem is that I can't get the new value of the views to show on that particular table row.
I have to refresh the page to see the updated views value and that's not what I want.
I want it updated dynamically as the button is clicked. This is what I am doing.
<table>
@foreach($data as $datum)
<tr>
<td><span class="view2">{{$datum->views}}</span></td>
<td><button class="btn btn-default views" value="{{$datum->game_id}}">click</button></td>
</tr>
@endforeach
</table>
<script>
$('.views').click(function (event) {
var game_id = this.value;
//alert(game_id);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type:'POST',
dataType:'JSON',
url:'{{url('home/update-views')}}',
data:'game_id='+game_id,
success:function(data)
{
var data=eval(data);
message=data.message;
var countSpan = $(this).parent().parent().find(".view2");
countSpan.text(message);
}
});
})
</script>