STepHan
STepHan

Reputation: 57

(Yii2) Variable not working from post

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?

Upvotes: 0

Views: 100

Answers (3)

STepHan
STepHan

Reputation: 57

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

Upvotes: 0

Gemark Almacen
Gemark Almacen

Reputation: 340

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

Upvotes: 1

Hearaman
Hearaman

Reputation: 8726

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

Upvotes: 0

Related Questions