简体   繁体   中英

Form not POST ing

this is my view where there is a form containig a field and a submit button.

<form id="typeheadForm" method="post" class="form-horizontal">
        <div class="form-group">
            <label class="col-xs-3 control-label">Movie</label>
            <div class="col-xs-3">
                <input type="text" class="form-control" name="state" id="movie_name" />
            </div>
        </div>

         <input type='submit' name='submit' value='Search' class="btn btn-squared btn-default">
    </form> 

and below is my controller code

  public function actionMovies_all()
        {
            $this->layout = "main";
            if ( isset( $_POST[ 'movie_name' ] ) )
            {
             print_r("success");die();
            }
            if ( Yii::$app->request->post() )
            {
              print_r(Yii::$app->request->post());die();
            }
        }

i am not able to POST the form. what am i doing wrong? i am getting an error " Bad Request (#400) Unable to verify your data submission."

Replace <form id="typeheadForm" method="post" class="form-horizontal"> with

<?= \yii\helpers\Html::beginForm('', 'post', ['id' => 'typeheadForm', 'class' => 'form-horizontal']);?>

You are getting bad request because when you create your form manually, you did not include csrf token into it. When you create form with Html::beginForm method it takes care about it internaly.

try this:

public function actionMovies_all()
    {
        $this->layout = 'main';
        if ( isset( $_POST[ 'submit' ] ) )
        {
         print_r('success');die();
        }
        if ( Yii::$app->request->post() )
        {
          print_r(Yii::$app->request->post());die();
        }
    }

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