Search code examples
formsyii2html-post

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."


Solution

  • 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.