Yii2通过AJAX将id发送到隐藏输入,并通过POST到达

时间:2017-04-19 06:44:46

标签: php jquery ajax yii2

我想将data.user_id发送到名为user_id的隐藏输入,然后在另一个html :: a elem中通过POST发送。 我的观点:

<?php

use yii\helpers\Html;
use yii\widgets\Pjax;

/* @var $this yii\web\View */
/* @var $searchModel app\models\PostSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

?>

<div class="post-index" style="position:relative;">
    <div class="content">
        <div class="row">
            <div class="col-md-12 search-post">
                <div class="col-md-4">
                    <?= Html::a('Create Post', ['create'], ['class' => 'btn btn-primary']) ?>
                    <?= Html::a('Search', [''], ['class' => 'btn btn-default search-form']) ?>
                    <?= Html::radioList('search-type', 'search-type',
                                            ['item1' => 'by title', 'item2' => 'by tag'],
                                            [
                                                    'style' => 'display: inline-block; margin-left: 10%'
                                            ]) ?>
                </div>
                <div class="col-md-8 search-input">
                    <?= Html::textInput('search-info', '',['class' => 'form-control']) ?>
                </div>
                <?php
                    $this->registerJs("
                        $('.search-form').on('click', function(event){
                            event.preventDefault();
                            var input = $('input[name=search-info]').val();
                            $.ajax({
                                type : 'GET',
                                url : '" . \yii\helpers\Url::to(['search-post']) . "?title='+input,
                                dataType : 'json',
                                success : function( data ){
                                    $('#posts').css('display', 'none');
                                    $('.none').fadeIn(1500);
                                    $('.title-post h3').html( data.title );
                                    $('.content-post').html( data.content );
                                    $('input[name=user_id]').val( data.user_id);
                                }
                            });
                        });
                    ");
                ?>
            </div>
        </div>
        <div class="row">

            <div class="none" style="display: none;">
                <div class="title-post text-center"><h3></h3></div>
                <div class="content-post"></div>
                <div class="foot-post">

                    <form action="" method="post">
                        <?= Html::hiddenInput('user_id', '') ?>
                    </form>

                    <div class="text-left">
                        <?= Html::a('Go To Post', ['view', 'id' => $_POST['user_id']]) ?>
                    </div>

                    <div class="text-right">
                        <?= Html::a('Back','index', ['class' => 'btn btn-warning']) ?>
                    </div>

                </div>
            </div>

            <?php Pjax::begin(['id' => 'posts']) ?>
            <div class="col-md-12">
                <?php

                    foreach ($posts as $post)
                    {
                        $id = $post->user_id;

                        $user = $userModel::find()->where(['id' => $id])->one();

                        echo "<div class='col-md-6 blog-post'>
                                  <div class='col-md-3 post-prof-img'>"
                                        . Html::img('../images/' . $user->image, ['alt' => 'image', 'class' => 'tall img-circle']) .
                                  "<p class='text-center title-par'><strong><em> $user->username </em></strong></p>
                                   <p class='text-center'> $post->date_create</p>
                                  </div>
                                  <div class='col-md-9 post-cont'><p>"
                                        . Html::a('Click for more!',['view', 'id' => $post->post_id]) .
                                        "</p><label class='text-center'> $post->title : </label>
                                        <div class='fade-post'>
                                            $post->content
                                        </div>
                                  </div>
                              </div>";
                    }
                ?>
            </div>
            <?php Pjax::end() ?>
        </div>
    </div>
</div>

我可以看到我的方式不可能,所以请提供一些建议!提前谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你对$_POST有错误的想法。简单地抛出Undefined index: user_id因为您尚未提交表单,因此$_POST['user_id']不存在。

您应该更改代码以使用不基于$_POST的普通变量。

<?= Html::a('Go To Post', ['view', 'id' => $idThatIsNotPOST]) ?>

如果您想使用ajax更改链接,则必须更改其href。类似于:$("a").attr("href", "<replace current href id with ajax's>")