[SOLVED]: stuck at submitting ajax-generated form in Yii

Solution here: http://www.yiiframework.com/forum/index.php?/topic/8219-solved-ajax-form-submission/

plus i put the action in the wrong controller… ;)

Hi there,

i’m having nice first days using Yii but have some troubles with getting things that i used to write ‘from the finger’ .

Here is the background for my question:

I created a ‘project’ model,controller and view (works fine) and in the ‘views/project/_form.php’ i put an ajax link that displays a partial view : a form that belongs to another model, named ‘picture’.

So, the ‘project’ _from.php has this inside:





<?php


echo CHtml::ajaxLink('Add picture', array('projekt/addPicture'), array( 'update'=>'#req_res'));


?>


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




the /project/addPicture action successfully shows the form using such code:




public function actionAddPicture()

        {


                $model=new Picture;

                $this->renderPartial('/projekt/_addPicture', array('model'=>$model));

                Yii::app()->end();

        }



and the ‘_addPicture.php’ file in the /projekt/view folder has a “cactiveform” inside, loading the ‘$model’ correctly (i can see my data stroed in the DB etc.)

Problem:

when i hit ‘save’ on the newly generated form, Yii sends me to a new white page containing again this form instead of saving it. Here’s the URL Yii sends me to:

http://localhost/projekt/addPicture?_=1304644637668

i am vaguely aware that it may have something to do with me not telling Yii which controller to use (?) but i don’t know how to specify the controller (if this is the case)

Can you point me to some direction here? Thank you

This solution does not work when js is disabled. Also in your code make sure render partial 3erd param is set to false and the 4th param to true so that the js code is returned with the form in the Ajax request.

from the controller call $this->renderPartial(’_form’, array(‘model’=>$model), false, true);

here is the view file for the form





$ajax = CHtml::ajax(array(

	'url' => $url,

	'type' => 'post',

	'dataType' => 'json',

	'data' => 'js:form.serialize()',

	'success' => 'js:function(data){ 

		// do some processing, here you can redirect, notify the user the form has been submitted, clear the form


	}',

	'error' => 'js:function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.responseText); }',

	'complete' => 'js:function(){ // do some processing  }'

));

?>


<div class="form">


<?php 

$form = $this->beginWidget('CActiveForm', array(

	'id'=>'post-form',

	'enableAjaxValidation'=>true,

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

		'validateOnType' => false,

		'validateOnChange' => false,

		'afterValidate'=>'js:function(form, data, hasError) {if (!hasError){ '.$ajax.'; return false; }}'

	),

	'action' => $url,

));

?>


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


	<div class="row">

		<?php echo $form->labelEx($model,'post_subject'); ?>

		<?php echo $form->textField($model,'post_subject',array('size'=>83,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'post_subject'); ?>

	</div>

	

	<div class="row buttons">

		<?php echo CHtml::submitButton('Submit'); ?>

	</div>


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


</div><!-- form -->