php - Yii2 : Vote for a Post in AJAX or Pjax -
i'm begginer in yii2 framework.
i work on forum :
in forum/posts method, there list of posts topic.
each post have score want , down in ajax.
in view posts.php
, open pjax block :
<?php pjax::begin(); ?> votes : <?= $val['score'] ?> <?= html::a('+', ['/post/voteup','id'=>$val['id']]) ?> <?= html::a('-', ['/post/votedown','id'=>$val['id']]) ?> <?php pjax::end(); ?>
in postcontroller :
public function actionvoteup($id){ //update request $postrepo=new postrepository(); $postrepo->vote('plus', "id=$id"); $post=$postrepo->getall("id=$id"); return $this->renderajax('vote', ['id'=>$id, 'score'=>$post[0]['score']]); }
you can see return vote.php view in ajax, same code pjax.
<?php pjax::begin(); ?> votes : <?= $score ?> <?= html::a('+', ['/post/voteup','id'=>$id]) ?> <?= html::a('-', ['/post/votedown','id'=>$id]) ?> <?php pjax::end(); ?>
the update request ok have problems/questions :
ex : want 2nd post score, click, ok, click second time, part of view refresh 1st post score (but in database, it's score updated). think problem part of view return in
actionvoteup()
. should return forum/posts or post/vote ?when click in link, url :
post/voteup
; how can return in original url forum/posts ?
i don't understand how works pjax, , didn't find examples utilisation.
thanks replies :)
you need add 'enablepushstate' => false
in pjax
attribute.
like
<?php pjax::begin(['enablepushstate' => false]); ?>
for more info. visit demo
Comments
Post a Comment