Home » Php » php – issue in sending form data to controller in yii2

php – issue in sending form data to controller in yii2

Posted by: admin July 12, 2020 Leave a comment


I want to send form data from my view to the controller to insert the data in the database. I’ve created the model and the controller but when I’m sending the form to the controller with the action. It just refreshed the page the data is not passed to the controller, just index page will be loaded. This is my form:

<div class="container">
 <!--<div style="text-align:right;" class="row">
   <h3>ارسال توییت جدید</h3>
   <div class="row">
   <div class="col-md-12" >
               <div class="widget-area no-padding blank">
               <div class="status-upload">
                 <!--<form method="POST" action="?r=twit/send-twit">-->
                 $form = ActiveForm::begin([
                   'options' => ['action' => '?r=twit/send-twit']
                  <?= $form->field($model,'twit')->textarea(); ?>
                   <!--<textarea name="twitContent" style="text-align:right;" placeholder="...توییت خود را وارد کنید" ></textarea>-->
                     <li><a title="" data-toggle="tooltip" data-placement="bottom" data-original-title="Audio"><i class="fa fa-music"></i></a></li>
                     <li><a title="" data-toggle="tooltip" data-placement="bottom" data-original-title="Video"><i class="fa fa-video-camera"></i></a></li>
                     <li><a title="" data-toggle="tooltip" data-placement="bottom" data-original-title="Sound Record"><i class="fa fa-microphone"></i></a></li>
                     <li><a title="" data-toggle="tooltip" data-placement="bottom" data-original-title="Picture"><i class="fa fa-picture-o"></i></a></li>
                   <!--<button name="submit" type="submit" class="btn btn-success green"><i class="fa fa-send"></i> ارسال توییت</button>-->
                   <?= Html::SubmitButton('ارسال',['class' => 'btn btn-success green']); ?>
                 <?php ActiveForm::end(); ?>
               </div><!-- Status Upload  -->
             </div><!-- Widget Area -->

and here is my controller action which is in twitController class and will not be executed and the data is not passed there:

  public function actionSendTwit()
        $request = Yii::$app->request;
        $sag = $request->post('twit');
        if ($request->post('twit')){
            $twitContent = $request->post('twit');
How to&Answers:

try like this

$form = ActiveForm::begin([
    'action' => ['controller/action'],

in place of this

$form = ActiveForm::begin([
               'options' => ['action' => '?r=twit/send-twit']

Refer Docs