2017-04-23 28 views
0

工作這是我簡單的index.php文件(Yii2)變量不能從後

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

所以我想點擊之後出現首先按鈕。我的錯誤在哪裏?

+0

php代碼被評估爲服務器端......所以在提交執行並且相關操作被調用後,post的內容可用..而這不是客戶端..如果您需要交互式服務器端你應該看看阿賈克斯 – scaisEdge

回答

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

我試過..沒有改變 – STepHan

+0

你是否提交表單到同一頁? – Hearaman

0

你應該做在你的控制器的要求,並做一些AJAX在您的視圖。