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…
samilo
(Samiloxphp)
July 2, 2013, 5:28am
2
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';
mirunho
(D Mirecki)
July 2, 2013, 5:44am
4
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…
mirunho
(D Mirecki)
July 2, 2013, 5:57am
6
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…
mirunho
(D Mirecki)
July 2, 2013, 6:09am
8
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.
mirunho:
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 $this->render
for AJAX response try to render partial content for AJAX call by using $this->renderPartial('partial-view-file');
mmbalaa
(Mmbalasundaram)
July 2, 2013, 9:50am
11
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.