Hi Follow my steps below…
Create a new action as follows
Step : 1 Create a new view file namely _ForumPost
<div id="list-of-post" class="list-post">
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
));
?>
</div>
Step :2 Rewrite your action as follows
public function actionIndex($id=NULL) {
$model = new ForumPost;
if(isset($_POST['ForumPost'])) {
$model->attributes=$_POST['ForumPost'];
$model->save();
$this->refresh();
// $this->redirect(Yii::app()->user->returnUrl);
}
$dataProvider=new CActiveDataProvider('ForumPost', array(
'sort'=>array(
'defaultOrder'=>'updated DESC',
)
));
if(isset($id)){
$this->renderPartial('_ForumPost',array('dataProvider'=>$dataProvider));
Yii::app()->end();
}
$this->render('index',array('dataProvider'=>$dataProvider,'model'=>$model));
}
Rewrite your index.php view file as follows
<div id="forumForm">
<div id="simple-div">
<?php echo $this->renderPartial('_uploadStatus', array('model'=>$model)); ?>
</div>
</div>
<hr style="height: 1px;"/>
<div id="list-of-post" class="list-post">
<?php
Yii::app()->runController('ForumPost/id/true');
?>
</div>
Rewrite your _uploadStatus.php view file as follows
<div class="form">
<?php $form=$this->beginWidget('CActiveForm'); ?>
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo $form->hiddenField($model,'user_id', array('value'=>Yii::app()->user->id), array('size'=>20,'maxlength'=>20)); ?>
</div>
<div class="row">
<?php echo $form->textArea($model,'content', array('placeholder'=>"What's going on...!", 'class'=>'status-txt-area')); ?>
</div>
<div>
<?php echo CHtml::Button('SUBMIT',array('onclick'=>'javascript:send();')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<script type="text/javascript">
function send()
{
var data=$("#your-form_id").serialize();
$.ajax({
type: 'POST',
url: '<?php echo Yii::app()->createAbsoluteUrl("ForumPost"); ?>',
data:data,
success:function(data){
jQuery("#list-of-post").load(<?php echo Yii::app()->createAbsoluteUrl("ForumPost/index?id=true"); ?>);
},
error: function(data) { // if error occured
alert("Error occured.please try again");
alert(data);
},
dataType:'html'
});
}
</script>
Finally write your ajax form submit code at your action…
Cheers!
If works don’t hesitate to press +