[SOLVED]Comparing Radio button value

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>

Your code seems to be working fine for me. What variable is the error for?

Hello Steve,

Here is slightly modified code. I want to compare the value of the selected radio button with the $x variable. But it is unable to compare. If the selected value is equal to $x then the value of “maxlim” should return “Yes”. Else it will return the value of “maxlim” “No”.

  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]);
	
	$model = new IncForm();
	$model->maxlim = ($model->name==$x) ? "Yes" : "No";
		if ($model->load(Yii::$app->request->post()) && $model->validate()) {
		// var_dump($model->name);
                // var_dump($model->maxlim);
                // die();

			if($model->save()){
		        return $this->refresh();
				}
			}
				return $this->render('test', [
							'answers'=>$answers,
      				     			'model' => $model,
      				     		]);

}

I’m not quite sure what you’re wanting to do with the result, but you should be assigning $maxlim after you load the posted data into your model. Maybe something like this:

if ($model->load(Yii::$app->request->post())) {
        $model->maxlim = ($model->x == $_POST['IncForm']['name']) ? "Yes" : "No";
        $model->save();
        return $this->refresh();
} else {
    $x=rand(100,999);
    $y=rand(1,9);        
    $answers=shuffle_arr([$x=>$x,$y=>$y]);
    $model->x = $x;
    return $this->render('test', [
        'answers'=>$answers,
        'model' => $model,
    ]);
}

You will need a way to preserve $x after the submit so you have something to compare to. You could put it in a hidden input or add $x attribute to your model.

Hello Steve,

Sorry for the delayed reply.

It works on my end with some modification with your guidance.

Thank you very much. Really appreciate your guidance.

This thread can be mark as solved.

Regards,
Siddhartha