doulun5683 2017-04-21 08:19
浏览 87
已采纳

Yii2在博客文章中添加标签

I am totally confused about how should i save tag_id and post_id in the table when I still don't have the post_id. I made dropdownlist with options from database table

<?= Html::activeDropDownList($model, 'post_id', $ddItems, ['class' => 'form-control dd-list', 'prompt' => '']) ?>

That list contains tags(PHP, Java etc.). User can have up to 3 tags on post. I tried to pass tags through GET and take in the controller with \Yii:$app->request->get(['id']) but without result.

my jQuery:

var ddList = $('.dd-list');
    var tagList = $('.tag-container');

    ddList.on('change', function () {
        var tagHolder = document.createElement('div');
        tagHolder.setAttribute('class', 'tag-holder');
        var selected = $('.dd-list option:selected').text();
        tagHolder.setAttribute('id', selected);
        console.log(selected);
        if(tagList.find('div').length > 2){
            alert('You can have most 3 tags!');
            return false;
        }

        if(tagList.find('#'+selected).length){
            return false;
        }else{
            tagHolder.append(selected);
            tagList.append(tagHolder);
            $.ajax({
                type : 'GET',
                dataType : 'text',
                url : '../post/create?tag=' + selected
            });
        }
    });

actionCreate :

public function actionCreate()
    {
        $model = new Post();
        $id = \Yii::$app->user->identity->id;
        $ddItems = ArrayHelper::map(Tag::find()->all(),'tag_id', 'tag_name');

        if ($model->load(Yii::$app->request->post())) {

            date_default_timezone_set('Europe/Berlin');

            $param = \Yii::$app->request->get();

            $model->user_id = $id;
            $model->date_create = date('m/d/Y');

            if($model->save())
            {
                $tag = \Yii::$app->request->get(['tag']);
                return $this->redirect(['view', 'id' => $model->post_id]);
            }

            throw new \yii\db\Exception("Some problem with DB connection ocurred!");

        } else {
            return $this->render('create', [
                'model' => $model,
                'ddItems' => $ddItems
            ]);
        }
    }

I am getting error : undefined tag. I am trying to get only one tag (because still don't know how to pass more). What is the right way to reach my goal? I am in total mess from about 1 day! Ton of thanks in advance!

EDIT I tried to send that tag with on click event like this :

$('.create-post').on('click', function () {
        var tag = $('input[name=chosen-tag]').val();
        console.log(tag);   
        $.ajax({
            type : 'GET',
            dataType : 'text',
            url : '../post/create?tag=' + tag
        });
    });
  • 写回答

1条回答 默认 最新

  • dongtuo3795 2017-04-21 08:42
    关注

    I could not understand your programming scenario, But..

    Change

     $tag = \Yii::$app->request->get(['tag']);
    

    To

     $tag = \Yii::$app->request->get('tag');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装