ajax edit or update form

hi friends ,

i can’t edit form using ajax (ajaxSubmitButton)

please complate sample (view/controller/model) for update form using ajax

thx

help me please :D

What exactly is not working for you?

[font="Tahoma"]

views/post/create.php


<h2> Create Post View </h2>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm'); ?>

 

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

 

    <div class="row">

        <?php echo $form->label($model,'title'); ?>

        <?php echo $form->textField($model,'title') ?>

    </div>

	

	<div class="row">

        <?php echo $form->label($model,'content'); ?>

        <?php

		$this->widget('application.extensions.cleditor.ECLEditor', array(

        'model'=>$model,

        'attribute'=>'content', //Model attribute name. Nome do atributo do modelo.

        'options'=>array(

            'width'=>'600',

            'height'=>250,

            'useCSS'=>true,

        ),

        'value'=>$model->content, //If you want pass a value for the widget. I think you will. Se você precisar passar um valor para o gadget. Eu acho ir?.

    ));

	?>

    </div>

	

	<div class="row">

        <?php echo $form->label($model,'permission'); ?>

        <?php echo $form->textField($model,'permission') ?>

    </div>


    <div class="row buttons">

                <?php

					echo CHtml::ajaxSubmitButton(

						($model->isNewRecord ? 'Create' : 'Save'),

						($model->isNewRecord ? array('post/addPost') : array('post/update') ),

						//array('post/addPost'),

						array(

							'update'=>'#req_res02',

						)

					);

				?>

	</div>

 

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

<div id="req_res02">...</div>

</div>

views/post/update.php


<?php

$this->breadcrumbs=array(

	$this->module->id,

);

?>

<?php echo $this->renderPartial('create', array('model'=>$model)); ?>

actionUpdate


public function actionUpdate()

	{

		$model=$this->loadModel();

		if(isset($_POST['post']))

		{

			$model->attributes=$_POST['post'];

			if($model->save())

			{

				echo "<h2> Post is Updated ! </h2>";

			}

		}

		$this->render('update',array('model'=>$model));

	}

		

	public function loadModel()

	{

		if($model===null)

		{

			if(isset($_GET['id']))

			{

				if(Yii::app()->user->isGuest)

					$condition='status='.Post::STATUS_PUBLISHED.' OR status='.Post::STATUS_ARCHIVED;

				else

					$condition='';

				$model=Post::model()->findByPk($_GET['id'], $condition);

			}

			if($model===null)

				throw new CHttpException(404,'The requested page does not exist.');

		}

		return $model;

	}

[/font]

update link : http://localhost/demo/abedi/post/update&id=2

but, when click save buttom not working update

The default value for jQuery AJAX is “GET” (for your reference), so if you want to use $_POST, set the type of ajaxSubmitButton to ‘POST’.

If you don’t want to change the type of AJAX submit, try this:




        //let's put $id here

        public function actionUpdate($id)

        {

                //loadModel should have an id so it knows what to load

                $model=$this->loadModel($id);

                //instead of $_POST, let's try $_GET

                if(isset($_GET['post']))

                {

                        $model->attributes=$_GET['post'];

                        if($model->save())

                        {

                                echo "<h2> Post is Updated ! </h2>";

                        }

                }

                $this->render('update',array('model'=>$model));

        }

                

        public function loadModel($id)

        {

                //where did $model come from?

                //if($model===null)

                //{                        

                        if($id)

                        {

                                if(Yii::app()->user->isGuest)

                                        $condition='status='.Post::STATUS_PUBLISHED.' OR status='.Post::STATUS_ARCHIVED;

                                else

                                        $condition='';

                                $model=Post::model()->findByPk($id, $condition);

                        }

                        if($model===null)

                                throw new CHttpException(404,'The requested page does not exist.');

                //}

                return $model;

        }



These were not tested, so just troubleshoot some more if there’s an error (hopefully the succeeding errors will be easier to solve).

Use a debugger with breakpoints, and use firebug to test the ajax calls from the browser.

not work :(

please complate sample (view/controller/model) for update form using ajax

There are many ajax form update samples in our wiki, just do a searching (this one,for example).

I don’t think you can expect someone to test the codes for you. If you want faster replies, just provide the error messages and only the necessary part of your codes, because it discourages busy people from reading a long post (or maybe its just me).

Check your yii runtime log or php/apache logs for errors, and use an IDE with debugger. And for ajax problems, use firebug so you can monitor ajax processes as it happens.

Just to be sure that there’s no problem with your controller codes, use submitButton instead of ajaxSubmitButton. Error will be prompted if YII_DEBUG is set to true.