(Yii2) Variable not working from post

空扰寡人 提交于 2019-12-25 12:45:19

问题


This is my simple index.php file

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$this->title = 'Qwert';
?>
<?php $variable=1; //that variable
$f=ActiveForm::begin() ?>

<?php
if($variable==1){
    echo Html::submitButton('First Button',['name'=>'b','value'=>1])."<br/>";
}else{
    echo Html::submitButton('Second Button',['name'=>'b','value'=>2]);}

if(Yii::$app->request->post('b')==='1' ) {$variable=2;}
if(Yii::$app->request->post('b')==='2' ) {$variable=1;} ?>

<?php ActiveForm::end() ?>

So I want to after click First button it appears Second. Where is my mistake?


回答1:


Keep bellow statements after $variable declaration.

  if(Yii::$app->request->post('b')==='1' ) {$variable=2;}
  if(Yii::$app->request->post('b')==='2' ) {$variable=1;}

Like

  $variable=1;
  if(Yii::$app->request->post('b')==='1' ) {$variable=2;}
  if(Yii::$app->request->post('b')==='2' ) {$variable=1;}



回答2:


You should do the request in your controller and do some ajax in your view.




回答3:


In my Controller, I have to add return.

 if(Yii::$app->request->post('b')==='1' ) {$variable=2; return $this->render('index');}
if(Yii::$app->request->post('b')==='2' ) {$variable=1;return $this->render('index');}


来源:https://stackoverflow.com/questions/43567773/yii2-variable-not-working-from-post

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!