Hello experts,
Can you guide me where i am going wrong with the below code.
I want to compare selected radio button value with some value. I am getting undefined variable error.
Model Code:
<?php
namespace frontend\models;
use Yii;
class IncForm extends \yii\db\ActiveRecord
{
//public $id;
// public $name;
// public $maxlim;
public static function tableName()
{
return 'test';
}
public function rules()
{
return [
[['name','maxlim'], 'safe'],
];
}
}
Controller Code:
<?php
namespace frontend\controllers;
use Yii;
use yii\base\Model;
use yii\web\Controller;
use frontend\models\IncForm;
class TestController extends Controller
{
public function actionTest(){
function shuffle_arr($arr){
$keys = array_keys($arr);
shuffle($keys);
foreach($keys as $key) {
$shuffeled[$key] = $arr[$key];
}
return $shuffeled;
}
$x=rand(100,999);
$y=rand(1,9);
$answers=shuffle_arr([$x=>$x,$y=>$y]);
//$maxlim = ($x == $answers) ? "Yes" : "No";
if ($answers==$x) {
$maxlim="Yes";
}else{
$maxlim="No";
}
$model = new IncForm();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->refresh();
}
return $this->render('test', [
'answers'=>$answers,
'model' => $model,
]);
}
View Code:
<div id= "content">
<?php $form = ActiveForm::begin([
'id' => 'form-test'])?>
<div class="col-sm-6">
<div class="qr" id="rlist">
<?= $form->field($model, 'name')->radioList($answers,['onchange'=>'document.getElementById("form-test").submit()'])->label(false); ?>
</div>
</div>
<?php ActiveForm::end() ?>
</div>