ajax form submission

I was going through the yii blog tutorial, and trying to make my own app. I kind of figured out the ajax validation. But I want to know how to do the ajax submission.

Here is the scenario: in one ‘ask’ webpage, there are a form to add a new ‘answer’ and a CActivelist to list all the ‘answers’ for that ‘ask’. I want to know how I can submit the form in the ajax way, then get the return message, then refresh the CActiveList, rather then refreshing the whole webpage.

Below is my code. Can anybody please tell me what I should do for the next step? Thanks a lot!!!

  1. view:

	<div class="form">


	<?php $form = $this->beginWidget('CActiveForm', array(

		'id' => 'answer-form',

		'enableAjaxValidation' => true,)); ?>


		<?php echo $form->errorSummary($answer); ?>


		<div class="row">

		<?php echo $form->labelEx($answer,'description'); ?>

		<?php echo $form->textArea($answer, 'description'); ?>

		<?php echo $form->error($answer,'description'); ?>

		</div><!-- row -->


		<?php echo CHtml::submitButton(

				Yii::t('app', 'Save')



	<?php $this->endWidget(); ?>

	</div><!-- form -->


	<?php if($model->answerCount>=1): ?>


		<h3><?php echo $model->answerCount.' answer(s)';?></h3>


		<?php $this->widget('zii.widgets.CListView', array(

			'id' => 'answer-list',

			'dataProvider'=>new CArrayDataProvider($model->answers, array(


				'pagination' => array(

					'pageSize' => 5,



			'itemView' => '/answer/_view',

			'summaryText' => '',



	<?php endif;?>


  1. controller:


	public function actionView($id) {

		$ask = $this->loadModel($id, 'Ask');

		$answer = $this->newAnswer($ask);



		$this->render('view', array(

			'model' => $ask,

			'answer' => $answer,




	protected function newAnswer($ask) {

		$answer = new Answer;


		if (isset($_POST['ajax']) && $_POST['ajax'] === 'answer-form') {

			echo GxActiveForm::validate($answer);




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

			$answer->attributes = $_POST['Answer'];

			if($ask->addAnswer($answer)) {

				Yii::app()->user->setFlash('answerSubmitted', 'A new Answer is added!');




		return $answer;