php - Yii2: How to send new variable from view to controller? -


i have table called persons id , name fields.

i have create.php view loads model called persons , want add checkbox called hascar show if person has car (so boolean condition).

then have send button send $model array of form controller need add hascar variable $model array.

but checkbox not column of persons table got errors because not part of model.

i added checkbox in way not working, of course.

<?= $form->field($model, 'hascar')->checkbox(); ?> 

is possible send hascar variable inside $model array? mean, how can send hascar variable controller when send button pressed?

you can't pass variable $model object orbit affiliated db table, right this. need pass variable controller via request method (get, post).

try :

yii::$app->request->post() 

for post, , :

yii::$app->request->get() 

for get.

also on form add checkbox html class component.

example:

controller:

... $hascar = yii::$app->request->post('hascar'); .... 

view:

... // use activeformjs here $this->registerjs(     $('#my-form').on('beforesubmit', function (e) {         if (typeof $('#hascar-checkbox').prop('value') !== 'undefined') {             return false; // false cancel submit         }     return true; // true continue submit }); $this::pos_ready, 'form-before-submit-handler' ); ... <?= html::checkbox('hascar', false, ['id' => 'hascar-checkbox', 'class' => 'form-control']) ?> ... 

more on activeformjs: enter link description here

i hope answer covered you.

damian


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -