I have created an AJAX request on button click event which updates a model field to true or false in order to mark model record as liked or disliked.
Context:
- When user access the view, it must show one or other button depending if the record has the mark of liked or unliked.
When user access the view, he will have multiple records and he can like or unlike any of them, so it's needed to post AJAX request and show/hide the correct button in the corresponing HTML element for that model record.
HTML Code (this is just one record instance, actual view can have multiple instances of this code based on model records amount)
<div class="kt-portlet__head-toolbar">
<a href="#" class="btn btn-icon" data-toggle="dropdown" onClick="likeProperty({{$match->matchid}})">
{{$match->matchid}}
@if ($match->prop_like == true)
<i class="flaticon2-bell kt-font-brand likeprop" id="likeprop" name="likeprop"></i>
@endif
@if ($match->prop_like == false)
<i class="flaticon2-bell-4 kt-font-brand unlikeprop" id="unlikeprop" name="unlikeprop"></i>
@endif
</a>
</div>
- Javascript Code
function likeProperty(matchid) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
method: 'post',
url: '/processes/offerdemand/matchlike/'+ matchid,
data: {
'id': matchid,
},
success: function () {
// hide like button
$('#likeprop').hide();
// show like button
$('#unlikeprop').show();
},
error: function (XMLHttpRequest) {
// handle error
}
});
}
Does anyone has idea on this type of requirement?
Regards