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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vscode的问题提问
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM