I'm still struggling with ajax form and Yii. I got the error Undefined variable: model. My form is renderPartial by another view renderPartial itself by another view.
Here the details
in my POST Controller
public function actionViewComment()
{
$post=$this->loadModel();
$comment=new Comment;
$this->renderPartial('_viewComment',array(
'model'=>$post,
'comment'=>$comment,
));
}
_viewcomment.php call the commment/_form.php
<?php $this->renderPartial('/comment/_form',array(
'model'=>$comment,
)); ?>
in the _form.php , the variable $model is accessible there is a button that call a jquery function send() when it is clicked
function send() {
var data=$("#comment-form").serialize();
$.ajax({
type: 'POST',
url: '<?php echo Yii::app()->createAbsoluteUrl("post/Ajax"); ?>',
data:data,
success:function(data){
alert("Data Saved");
},
error: function(data) { // if error occured
alert("Error occured.please try again");
alert(data);
},
dataType:'html'
});
}
The actionAjax of the post controller contains
public function actionAjax()
{
if(isset($_POST['Comment']))
{
if($model->validate())
{
$model->save();
return;
}
}
}
In action Ajax , I got the error Undefined variable: model. I dont understand why?
If I use $model=new Comment; the data is not saved because $model is empty
Can you explain?
by adding
$model=new Comment;
$model->attributes = $_POST["Comment"];
I go the following error
DbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 19 tbl_comment.status may not be NULL. The SQL statement executed was: INSERT INTO 'tbl_comment' ("author", "email", "url", "content", "create_time") VALUES (:yp0, :yp1, :yp2, :yp3, :yp4) (C:\wamp\www\yii\framework\db\CDbCommand.php:358)</p><pre>#0 C:\wamp\www\yii\framework\db\ar\CActiveRecord.php(1077): CDbCommand->execute()
Thank you in advance for your help.