Undefined Variable: Res

HI, i’m using Ajax Call in my application. i given AjaxRequest in controller and i created variable to display value.But i’m getting this error

"Undefined Variable". Can i know how to define "res" variable.




function myfunction(v){

     			//alert(v);

                	var range=$('#rangeVal').val();

                	$.ajax({

                        	url: '<?php echo Yii::app()->createAbsoluteUrl("usersStatsInfo/myGraph"); ?>',

                        	type: 'POST',

                    	//   async: false,

                    	data: 'ans='+v+'&value2='+range,

                    	success: function(data)

                    	{

                        	//alert(res);

                        	//alert("inside success");

                 			$("#drop").html(data);

                    	}

                    	});

                  	}



Controller:




 if(Yii::app()->request->isAjaxRequest) {

                        	if(isset ($_POST['ans'])) {

                            	$res = $_POST['ans'];

                        	}

                            	}

                	$this->render('myGraph', array('res'=>$res));



And view page:




<?php echo $res; ?>



.

Pls help me where i did mistake and how to define variable and display the value…

you can define it by add


public $res  

Add it to your controller in top like :


class MyController extends CController{   public $res= 'bar';    public function actionIndex(){ 	$this->render('index');   } }

Access it by :


$this->render('graphic', array('res'=>$this->res)); 

Now also i’m getting same error in this line…


  $this->render('graphic', array('res'=>$res));

Can i know how to mention dynamically here…




public $res = 'weight';



It could be some problem with your post method, in case if $_POST[‘ans’] is not set $res is not defined, try to add


$res = '' //some value 

before your if statement

if i define before if condition like


 $res = 'weight'

it displaying only what value i given in $res. But i need to fetch data dynamically…

so there is some problem with your request and $_POST did You var_dump it ?

ya, i tried var_dump also but this way also i’ getting error…

You can try change your data to :


           data: { "ans": v, "value2": range },

And you should probably check your post params with firebug or something like that.

inside if condition my $res its not working, it saying only undefined variable. My post params are all correct.

Try this:

Controller:




 if(Yii::app()->request->isAjaxRequest) {

   if(isset ($_POST['ans'])) {

	// Render for actual content

	$res = $_POST['ans'];

	$this->render('myGraph', array('res'=>$res));

  } else {

	// Render other view for error if no post data found

	$this->render('error-view');

  }

}



Suggestion: In your code you are using &#036;this-&gt;render for AJAX response try to render partial content for AJAX call by using &#036;this-&gt;renderPartial('partial-view-file');

your rendering method and view is very clear. Just define $res before the ‘if’ condition. If your php is strict mode, it will show like this error.