vivekcu
(Vivekcu 08)
1
i have the following code in controller
[b]public function actionTest($id)
{
$test=array(1,2,3);
print_r($test);
}[/b]
in index.php
[b]<?php
$form = $this->beginWidget(‘CActiveForm’, array(
'id' => 'job-form',
'enableAjaxValidation' => true,
));
echo CHtml::ajaxSubmitButton(‘Save’, $this->createUrl(‘site/test’, array(‘id’ => 12)), array(
'type' => 'POST',
‘dataType’ => ‘json’,
'success' => 'function(data){
alert(data);
}’,
));
$this->endWidget();
?>
[/b]
but i can’t get the alert to work and the array is not getting displayed…
if i comment the datatype it works fine…
ani
(Aneesh)
2
In the controller add this and check
public function actionTest($id)
{
//$test=array(1,2,3);
//print_r($test);
echo CJSON::encode(array(
'status'=>'success'));
Yii::app()->end();
}
You can check the return in index.php like this
echo CHtml::ajaxSubmitButton('Save', $this->createUrl('site/test', array('id' => 12)), array(
'type' => 'POST',
'dataType' => 'json',
'success' => 'function(data){
if(data.status=="success")
{
alert("Your Message");
}
alert(data);
}',
));
vivekcu
(Vivekcu 08)
3
thanks a lot for the rep… it worked…
srm
(S Rmohammadi)
4
Hey Guys,
I have another question. i want to add feedback to my site. I put the following codes in my post controller:
public function actionPutFB($id)
{
$p=$this->loadModel();
$f = new Feedback();
$f->author_id = Yii::app()->user->id;
$f->post_id = $p->id;
try {
if ($f->save()) {
$p = Post::model()->findByPk( $f->post_id );
$p -> sumFeedback();
echo CJSON::encode(array('status'=>'success'));
}
// else echo json_encode($sr=false);
} catch (Exception $e) {
throw new CHttpException(400,'sara');
}
}
and the following in appropriate view:
<div id="feedback">
<?php
echo CHtml::ajaxLink(‘UnLike’ ,
$this->createUrl('post/PutFB'),
array(
'type'=>'POST',
'dataType'=> 'json',
'success' => 'function(data)
{
if(data.status=="success"){
$("#feedback").val(parseInt($("#feedback").val() , 10)+1);
}
}',
));
echo $model->feedback;
?>
</div>
but when press like link nothings happened. could you help me?