Yii2 dropdownlist onchange URL redirection

I would like this code to redirect / user / create How to do this?

echo Html::beginForm();

echo Html::activedropDownList(
    $model, //name
    'COMPANY_ID',  //select
    $companyList, //items
    ['onchange'=>'this.form.submit()'] //options
);

echo Html::endForm();`
+4
source share
3 answers

By default, the form will have action = ''and method = 'post'. You should change this:

echo Html::beginForm(['method' => 'get', 'action' => 'user/create']);

echo Html::activedropDownList(
    $model, //name
    'COMPANY_ID',  //select
    $companyList, //items
    ['onchange'=>'this.form.submit()'] //options
);

echo Html::endForm();

Further information can be found in the document here .

UPDATE

By the way, you should double-check how you retrieve the identifier on the user / creation page. To check how the get parameter is sent, I would recommend that you do var_dump ($_GET)in this action. IE:

ContacForm, "get", var_dump ($_GET) , :

array (size=3)
    'r' => string 'site/contact' (length=12)
    'ContactForm' => 
        array (size=5)
        'name' => string 'c' (length=1)
        'email' => string 'teste@teste.com.br' (length=18)
        'subject' => string 'safd' (length=4)
        'body' => string 'asfas' (length=5)
        'verifyCode' => string 'eaxpzwp' (length=7)
        'contact-button' => string '' (length=0)

, , "ContactForm". , ContactForm.php. /create . , - :

$get = Yii::$app->request->get(); //retrieve all get params in the url, even the route.
if (isset($get['ContactForm'])) { //Replace by the name of the form model.
    $COMPANY_ID = $get['ContactForm']['COMPANY_ID'];
}

, -, .

+3

, /create, beginForm

echo Html:beginForm(['users/create'])

http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#beginForm()-detail.

+3

... 'onchange' = > 'window.location = "/user/.." '; ...

0

Source: https://habr.com/ru/post/1606520/


All Articles