(Yii2)变量不能从post开始工作

(Yii2)变量不能从post开始工作,post,yii,yii2,Post,Yii,Yii2,这是我的simple 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 Butto

这是我的simple 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() ?>


因此,我想在单击第一个按钮后显示第二个按钮。我的错误在哪里?

$variable
声明之后保留下面的语句

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


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

在我的控制器中,我必须添加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');}

php代码在服务器端进行评估。。。因此,在执行提交和调用相关操作后,post的内容可用。。而这不是客户端。。如果你需要一个交互式的服务器端,你应该看看Ajax。你是否将表单提交到同一页面?
 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');}