简体   繁体   中英

yii2 hidden input field using javascript append and get it from controller

I am new to yii2. Appreciate if someone could help me solved a problem.

I am trying to append a hidden input field to my form using javascript (not link with model). After which, when I submit, get it from controller. However, there seem to be some mistake i did. Please advise and show me the correct code. The following is my code:

Javascript

var div_input = $('#div_hidden_input');
div_input.append('<input type="hidden" id="input1" value="value1" />');

Controller

if (!empty($_POST["input1"]))
{
    Yii::$app->getSession()->setFlash('error', "in");
}else{
    Yii::$app->getSession()->setFlash('error', 'out');
}

It always go into the else statement. I inspect the html code, the appended hidden field is there. Please help!

You have not given name attribute to it:

var div_input = $('#div_hidden_input');
div_input.append('<input type="hidden" id="input1" value="value1" name="input1"/>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM