简体   繁体   中英

How Do I loop through this POST array?

I have data that is being passed through an POST method. However when I try to get some data out of it and set it to a session variable, there is no data in it yet when I print_R($_POST) I see there is Data in it.

This is What I'm getting when I print_r() the POST data.

Array ( 
    [_csrf] => nXRvIHfHWeao64YBdwcdFJa3fz-KShIyAuHDNtKQqhCkDRdwErUqkOrSyHQQamtR5cBHWc57QUNq16hflaftKQ== 
    [LoginForm] => Array (
        [compayname] =>Termite Soup 
        [username] => Jim.Bot
        [password] => 123456 
        [url] => 
    ) 
    [login-button] => 
)

This is what I've tried.

This is the form where form is being filled.

<?php $form = ActiveForm::begin([
        'id' => 'login-form',
        'layout' => 'horizontal',
        'fieldConfig' => [
            'template' => "{label}\n<div class=\"col-lg-10\">{input} 
        </div>\n<div class=\"col-lg-8\">{error}</div>",
            'labelOptions' => ['class' => 'col-lg-3 control-label'],
        ],
     ]); 
    ?>

        <?php echo $form->field($model, 
  'compayname')>dropDownList(['GF_TB_TNT' => 'GF-TNT', 
        'Chicken Soup' =>'Dog Soup',
        'Termite Soup' =>'Termite Soup',
        ],

        ['prompt'=>'Select Company']); ?>



        <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>

        <?= $form->field($model, 'password')->passwordInput() ?>

        <?= $form->field($model, 'url')->hiddenInput()->label(''); ?>
        <div class="row">
           <div class="btn-group">
                <!-- <div class="col-md-2">
                </div> -->
                <div class="col-md-6">
                    <?= Html::submitButton('Login', ['class' => 'btn btn-primary pull-left', 'name' => 'login-button','style'=>'font-size: 15px;']) ?>
                </div>

Getting the data and setting it to a Session Variable

if (Yii::$app->request->post()) {
                $companyname = Yii::$app->request->post('compayname');

}

I want to set the companyname in the POST to a session Variable

What you can do is

foreach (Yii::$app->request->post('LoginForm') as $field) {
// some logic
}

Also I see a mistake in compayname. A "n" is missing in the word. But you can access it's value by Yii::$app->request->post('LoginForm')['compayname'];

You can shortly get this.

$arr = [];
foreach ($_POST as $param) {
    if (is_array($param)) {
        foreach ($param as $name) {
            if ($name == 'LoginForm') {
                array_push($arr, $name);
            }
        }
    } else {
    }
}

And you can figure out how the data is structured using var_dump

var_dump($arr);

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