Reputation: 705
I'm trying to create votes for my content in Yii2, so I'm trying to insert data with Ajax using "a" tag and widget but with ajax it's not working , without ajax it's working perfect. but when I use Ajax it's not working and I have no errors in console. my code for view is :
<script type="text/javascript">
$('document').ready(function(){
$('#btn-vote-up' + <?= $pst_id ?>).on("click", function(e){
$.ajax({
type: "POST",
data: {"value" : "Like"},
success: function(msg) {
console.log (<?= $pst_id ?>);
$('#note-up' + <?= $pst_id ?>).load(' #note-up' + <?= $pst_id ?>);
}
});
e.preventDefault();
});
});
</script>
<?= Html::a('<i class="ly-ic-favorite-plus"></i>', '#', [
'class' => 'btn-logout',
'id' => 'btn-vote-up'.$pst_id,
'data' => [
'params' => [
'value' => 'Like',
'pstIDL' => $pst_id,
],
],
]) ;
?>
<?php yii\widgets\Pjax::begin(['id' => 'note-up'.$pst_id]) ?>
<span><?= $total_up ?></span>
<?php yii\widgets\Pjax::end() ?>
And my widget code is :
if (Yii::$app->request->post('value') == 'Like')
{
$pstIDL = Yii::$app->request->post('pstIDL');
$modelLIKEPOST = $this->findLikePost($pstIDL);
AxVotePost::VoteUP($modelLIKEPOST, $this->usr_rid, $this->chn_id, $pstIDL);
header('Location: ' .Url::current() );
exit;
}
Upvotes: 1
Views: 542
Reputation: 23778
You are not sending the $pstIDL
value when making an ajax call and only value
is being sent, you can use var data=$(this).data('params')
to get both pstIDL
, and the value
json
and send it along the data in the call see below
<script type="text/javascript">
$('document').ready(function(){
$('#btn-vote-up' + <?= $pst_id ?>).on("click", function(e){
var data= $(this).data('params');
$.ajax({
type: "POST",
data: data,
success: function(msg) {
console.log (<?= $pst_id ?>);
$('#note-up' + <?= $pst_id ?>).load(' #note-up' + <?= $pst_id ?>);
}
});
e.preventDefault();
});
});
</script>
Upvotes: 1