1

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;
            }
4
  • saying it not working is not enough, what happens, when you send an ajax call, do you receive a 404 bad request or anything? and above all you are not sending the $pstIDL value when making an ajax call and only value is being sent. Commented Aug 1, 2018 at 15:04
  • I don't get any error no thing happen Commented Aug 1, 2018 at 15:13
  • the answer help me; thank you so much for your time this is the 3rd question you try to help me; thank you so much Commented Aug 1, 2018 at 15:36
  • 1
    :) we are all here to help each other @Bynd , you are always welcome. Commented Aug 1, 2018 at 15:42

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I tried your code but still not working; console.log pstIDL it's give "undefined".
I changed you code from var pstIDL=$(this).data('pstIDL'); to : var pstIDL = $(this).data('params'); and it's working; please can you edit your answer and change only var pstIDL=$(this).data('pstIDL'); to : var pstIDL = $(this).data('params');
yeah you can do it that way to get the whole json string and send as is @Bynd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.