Hi,
I am a newbie to Web Dev. I am trying to implement the Ajax operation following the post http://www.yiiframework.com/wiki/388/ajax-form-submiting-in-yii/. When I submit the form, Student request does not get posted however if I include a list widget based on the same model(Student), the list shows and form gets submitted showing the desired results. Just can’t figured out what is wrong where. One thing I observed, in the link above, the PersonForm extends from CFormModel where as my model Student extends from CActiveRecord. Based on this observation, I replicated the same but all in-vain.
In the code below, the blessed list widget is on the last line in create.php view file.
Following is my code:
Model -> Student.php
<?php
/**
* This is the model class for table "student".
*
* The followings are the available columns in table 'student':
* @property integer $id
* @property string $Student_name
* @property string $student_dob
*
* The followings are the available model relations:
* @property StudentSubject[] $studentSubjects
*/
class Student extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Student the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'student';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('Student_name', 'length', 'max'=>100),
array('student_dob', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, Student_name, student_dob', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'studentSubjects' => array(self::HAS_MANY, 'StudentSubject', 'stu_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'Student_name' => 'Student Name',
'student_dob' => 'Student Dob',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('Student_name',$this->Student_name,true);
$criteria->compare('student_dob',$this->student_dob,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
View -> create.php
<?php
/* @var $this StudentController */
/* @var $model Student */
$this->breadcrumbs=array(
'Students'=>array('index'),
'Create',
);
$this->menu=array(
array('label'=>'List Student', 'url'=>array('index')),
array('label'=>'Manage Student', 'url'=>array('admin')),
);
?>
<h1>Create Student</h1>
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
// THIS IS THE WIDGET WHICH LETS MY FORM GET POSTED......
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
View -> _form.php
<script type="text/javascript">
function send()
{
var data=$("#student-form").serialize();
$.ajax({
type: 'POST',
url: '<?php echo Yii::app()->createAbsoluteUrl("student/create"); ?>',
data:data,
success:function(data){
alert("success:"+data);
},
error: function(data) { // if error occured
alert("Error occured.please try again");
alert(data);
},
dataType:'html'
});
}
</script>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'student-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array(
'onsubmit'=>"return false;",/* Disable normal form submit */
'onkeypress'=>" if(event.keyCode == 13){ send(); } " /* Do ajax call when user presses enter key */
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'Student_name'); ?>
<?php echo $form->textField($model,'Student_name',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'Student_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'student_dob'); ?>
<?php echo $form->textField($model,'student_dob'); ?>
<?php echo $form->error($model,'student_dob'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::Button('SUBMIT',array('onclick'=>'send();')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller -> create:
public function actionCreate()
{
$model=new Student;
$dataProvider=new CActiveDataProvider('Student');
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
Yii::log('loging-01');
if(isset($_POST['Student']))
{
Yii::log('loging-02');
if($model->validate())
{
Yii::log('loging-03');
// form inputs are valid, do something here
print_r($_REQUEST);
return;
}
Yii::log('loging-04');
//$model->attributes=$_POST['Student'];
//if($model->save())
//$this->redirect(array('view','id'=>$model->id));
}
Yii::log('loging-05');
$this->render('create',array(
'model'=>$model,
'dataProvider'=>$dataProvider,
));
}