TheDevWay
TheDevWay

Reputation: 1413

form sending null value to the controller in yii2

here is my form and I'm sending it to the controller to get the data and insert it to the db with model but when I'm checking the post data in the controller it is null . here is my form :

<div class="container">
   <div class="row">
   <div class="col-md-12" >
               <div class="widget-area no-padding blank">
               <div class="status-upload">
                 <?php
                 $form = ActiveForm::begin([
                   'action' => ['twit/send-twit'],
                 ]);
                 ?>
                  <?= $form->field($model,'twit')->textarea(); ?>
                   <?= Html::SubmitButton('ارسال',['class' => 'btn btn-success green']); ?>
                 <?php ActiveForm::end(); ?>
               </div><!-- Status Upload  -->
             </div><!-- Widget Area -->
           </div>
   </div>
</div>

and here is my controller which I'm checking the value of the posted field (twit) and it is always null :

    public function actionSendTwit()
    {
        $request = Yii::$app->request;
var_dump($request->post('twit'));
die();
        if ($request->post('twit')){
            $twitContent = $request->post('twit');
            Twit::sendTwit($twitContent);
            \yii::$app->response->redirect('?r=twit/index',301)->send();
        }

    }

Upvotes: 0

Views: 2351

Answers (2)

memo
memo

Reputation: 345

more clean way

public function actionSendTwit()
{
    if ($this->request->isPost) {
        $form_data = Yii::$app->request->post('Twit');//same as model name
        $twitId = $form_data['id'];
        $twitCtn = $form_data['content'];
    }
}

Upvotes: 0

Nana Partykar
Nana Partykar

Reputation: 10548

Assuming Twit is model name.

public function actionSendTwit()
{
  $modelTwit = new Twit();
  $request = Yii::$app->request;

  if ($modelTwit->load($request->post())) {

    $twitContent = $request->post('Twit');
        var_dump($twitContent);
    Twit::sendTwit($twitContent);
    \yii::$app->response->redirect('?r=twit/index',301)->send();
  }

}

For User's Requirement.

View

Now suppose, you are having two field. username and twit.(I assume these field are present in your DB Table.) So, When you use $twitContent = $request->post('Twit');. It uses Twit Model not the field name.

If you want to check values of username and twit. Use below method.

<div class="container">
   <div class="row">
         <div class="col-md-12" >
             <div class="widget-area no-padding blank">
               <div class="status-upload">
                 <?php
                 $form = ActiveForm::begin([
                   'action' => ['twit/send-twit'],
                 ]);
                 ?>
                  <?= $form->field($model,'twit')->textarea(); ?>
                  <?= $form->field($model,'username')->textinput(); ?>
                  <?= Html::SubmitButton('ارسال',['class' => 'btn btn-success green']); ?>
                 <?php ActiveForm::end(); ?>
               </div><!-- Status Upload  -->
             </div><!-- Widget Area -->
           </div>
   </div>
</div>

Controller

<?
public function actionSendTwit()
{
  $modelTwit = new Twit();
  $request = Yii::$app->request;

  if ($modelTwit->load($request->post())) {

    $twitContent = $request->post('Twit'); //In this, model is called

    //If you want to see value then.
    print_r($twitContent['twit']);
    print_r($twitContent['username']);

    Twit::sendTwit($twitContent);
    \yii::$app->response->redirect('?r=twit/index',301)->send();
  }

}
?>

Upvotes: 3

Related Questions