【发布时间】:2014-02-07 17:04:59
【问题描述】:
我还在为 ajax 表单和 Yii 苦苦挣扎。 我收到错误未定义变量:模型。我的表单是由另一个视图渲染部分由另一个视图渲染部分本身。
这里有详细信息
在我的 POST 控制器中
public function actionViewComment()
{
$post=$this->loadModel();
$comment=new Comment;
$this->renderPartial('_viewComment',array(
'model'=>$post,
'comment'=>$comment,
));
}
_viewcomment.php 调用commment/_form.php
<?php $this->renderPartial('/comment/_form',array(
'model'=>$comment,
)); ?>
在 _form.php 中,变量 $model 是可访问的 有一个按钮在单击时调用 jquery 函数 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'
});
}
post控制器的actionAjax包含
public function actionAjax()
{
if(isset($_POST['Comment']))
{
if($model->validate())
{
$model->save();
return;
}
}
}
在 Ajax 操作中,我收到错误未定义变量:模型。我不明白为什么?
如果我使用 $model=new 评论;数据没有保存,因为 $model 是空的
你能解释一下吗?
通过添加
$model=new Comment;
$model->attributes = $_POST["Comment"];
我出现以下错误
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()
提前感谢您的帮助。
【问题讨论】: