0

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.

1 Answer 1

1

In your actionAjax, you have to create model object for Comment. You didn't create $mode but you are calling their functions.

public function actionAjax()
    {
        if(isset($_POST['Comment']))
        {
           $model=new Comment;
           $model->attributes = $_POST["Comment"];
           if($model->validate())
            {
               $model->save();
               echo "saved";
            }
            else
            {
               echo "Failed";
            }
        } 
     }
Sign up to request clarification or add additional context in comments.

12 Comments

I did already but in this case, the data is not saved. I looked to the firebug and data sent is empty. @kumar_v
So your post is empty?
No , it is not. I can see in firebug that $_POST is equal to my data entered @kumar_v
The response is stil empty in firebug. @kumar_v
try updated code. however you will get some response from ajax. so if you get failed, then validation failed. then check your model validation and data
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.