Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue in sending form data to controller in yii2

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>-->
   <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">-->
                 <?php
                 $form = ActiveForm::begin([
                   'options' => ['action' => '?r=twit/send-twit']
                 ]);
                 ?>
                  <?= $form->field($model,'twit')->textarea(); ?>
                   <!--<textarea name="twitContent" style="text-align:right;" placeholder="...توییت خود را وارد کنید" ></textarea>-->
                   <!--<ul>
                     <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>
                   </ul>-->
                   <!--<button name="submit" type="submit" class="btn btn-success green"><i class="fa fa-send"></i> ارسال توییت</button>-->
                   <?= Html::SubmitButton('ارسال',['class' => 'btn btn-success green']); ?>
                 <!--</form>-->
                 <?php ActiveForm::end(); ?>
               </div><!-- Status Upload  -->
             </div><!-- Widget Area -->
           </div>
   </div>
</div>

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');
        die();
        if ($request->post('twit')){
            $twitContent = $request->post('twit');
            Twit::sendTwit($twitContent);
            \yii::$app->response->redirect('?r=twit/index',301)->send();
        }
    }
like image 445
TheDevWay Avatar asked Dec 23 '15 07:12

TheDevWay


1 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

like image 135
Bloodhound Avatar answered Oct 11 '22 21:10

Bloodhound