簡體   English   中英

Yii2通過AJAX將ID發送到隱藏的輸入並通過POST到達

[英]Yii2 send id to hidden input via AJAX and reach it through POST

我想將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>

如我所見,這是我無法嘗試的方法,請提供一些建議! 先感謝您!

我認為您對$_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>")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM