怀疑URL中的参数

时间:2015-04-08 23:38:20

标签: php yii2

我在下面的地址中有以下表格:

http://localhost/myapp/web/index.php?r=cashbook%2Faccomplishment

$form = ActiveForm::begin([
        'id' => 'accomplishment-form',
        'options' => ['class' => 'form-horizontal'],
        'action' => Url::to(['/cashbook/accomplishment']),
                'method' => 'get',
]);
$form->field($model, 'category_id')->dropDownList(ArrayHelper::map(
                Category::find()->where(['user_id' => Yii::$app->user->identity->id])
                                ->orderBy("desc_category ASC")
                                ->all(), 'id_category', 'desc_category'),
                ['onchange'=>'this.form.submit()','prompt'=>'-- Select --']); 

ActiveForm::end();

谁处理并生成以下网址:

http://localhost/myapp/web/index.php?r=cashbook%2Faccomplishment&Cashbook%5Bcategory_id%5D=18

但我只需要 category_id 值(在dropDownList中选择)。也就是说,URL应为:

http://localhost/myapp/web/index.php?r=cashbook%2Faccomplishment&category_id=19

如何保持这种方式或只获得 category_id

1 个答案:

答案 0 :(得分:0)

在视图中添加registerJs

$this->registerJs('var submit = function (val){
if (val > 0) {
    window.location.href = "' . Url::to(['/cashbook/accomplishment']) . '/?category_id=" + val;
}
}', View::POS_HEAD);

不要使用ActiveForm::begin...和所有代码。而不是使用这个:

echo Html::activeDropDownList($model, 'id_category', ArrayHelper::map(
            Category::find()->where(['user_id' => Yii::$app->user->identity->id])
                            ->orderBy("desc_category ASC")
                            ->all(), 'id_category', 'desc_category'), ['onchange'=>'submit(this.value);','prompt'=>'-- Select --']);