(Yii2)变量不起作用

时间:2017-04-23 05:31:17

标签: post yii yii2

这是我简单的index.php文件

<?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() ?>

所以我想在点击第一个按钮后显示第二个。我的错误在哪里?

3 个答案:

答案 0 :(得分:0)

$variable声明之后保留下面的声明。

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

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

答案 1 :(得分:0)

您应该在控制器中执行请求并在视图中执行一些ajax。

答案 2 :(得分:0)

在我的控制器中,我必须添加返回。

 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');}