Ajax Submit button

Hello ppl,

I will try to be give as may information as possible.

I have a model person where in _form.php view i have this code:




<div id="doy">

<?php echo $form->dropDownList($person,'doyid',CHtml::listData(Doy::model()->findAll(),'doyid','doyname'),array('prompt'=>'Select')); ?>

<?php echo CHtml::ajaxLink("Add new Doy.",$this->createUrl('/doy/addnew'),array('replace'=>'#doy'));?>

</div>

So there is a dropdownlist feeded with data from an other model Doy and a ajaxLink in case you can not find what you want and be able to add it with the function of the models (doy) addnew action which is the following:




	public function actionAddnew()

	{

		$model=new Doy;

                $this->performAjaxValidation($model); // I want to perform validation

                if(isset($_POST['Doy'])){

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

                    if($model->save()){

                        echo "test";

                    }

                } else {

                    $this->renderPartial('_form_1',array('doy'=>$model),false,true);

                }

        }



For the moment keep in mind the echo &quot;test&quot;; line.

The very first time when the actionAddnew will be called the _form_1 will be rendered.Here is _form_1.php view:


<div class="wide form" >

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

	'id'=>'doy-form',

	'enableAjaxValidation'=>true,

)); ?>

	<?php echo CHtml::errorSummary($doy); ?>


	<div class="row">

		<?php echo $form->labelEx($doy,'doyid'); ?>

		<?php echo $form->textField($doy,'doyid',array('size'=>20,'maxlength'=>20)); ?>

		<?php echo $form->error($doy,'doyid'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($doy,'doyname'); ?>

		<?php echo $form->textField($doy,'doyname',array('size'=>60,'maxlength'=>90)); ?>

		<?php echo $form->error($doy,'doyname'); ?>

	</div>

	<div class="row buttons">

		<?php echo CHtml::ajaxSubmitButton(Yii::t('forms','Create'),CHtml::normalizeUrl(array('doy/addnew'),array('replace'=>'#doy'))); ?>


	</div>

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

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

Ok…where is the problem. When i visit the _pform there is the dropdown list (codesnippet1) when i clik on the ajaxLink there the form renders proper replacing the dropdownlist. I insert values it validate them and i press the create of the _form_1 which is the ajaxSubmitButton. The values are submiting but the form is not replaced as it should from the echo "test";

Of course i dont want only test to be rendered but in fact i want a new dropdownlist with the values submitted previously to be the selected. Please help because i am new to ajax and yii.

What is the problem? If you want more information please inform.

For a start, try ‘update’ instead of ‘replace’. (update will replace the div tag with the renderPartial content).

/Tommy

Ok i changed in both ajax the replace to update. It didn’t happened what i was expecting. I used firephp so i can debug it and show you. I changed the actionAddnew like this…


	public function actionAddnew()

	{

		$model=new Doy;

                $this->performAjaxValidation($model);

                if(isset($_POST['Doy'])){

                    

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

                    if($model->save()){

                        echo "test";


                    }

                } else {

                    fb("edw giati???"); //debuging

                    $this->renderPartial('_form_1',array('doy'=>$model),false,true);

                }

        }

[list=1]

[*]Load page

[*]Then click the ajaxLink and renders.

[*]Then i enter some values to the textfield and validation occurs.

[*]And finally i press create.

[/list]

At final step i get the $this->renderPartial(’_form_1’,…); althought it’s in else case, strange!!! and of course test.

What i noticed is that the renderPartial comes with GET and the test with POST i will see if this is a problem now.But how can i changes this? :huh:

Any ideas welcome.

Sorry about the size of the pics but i didn’t know how to resize it smaller here in forum.

Something i noticed now is that both ajaxLink and ajaxSubmitButton come with id ‘yt0’ is this maybe the problem?

You can try setting a unique name attribute, htmlOptions is the fourth parameter to ajaxLink/ajaxsubmitButton




array('id'=>'unique_name')



Edit: should be ‘id’, not ‘name’

/Tommy

I left the ajaxLink (1st) to update and the ajaxSubmitButonn (2nd) at replace like.

_form


<?php echo CHtml::ajaxLink("Add new Doy.",$this->createUrl('/doy/addnew'),array('update'=>'#doy'));?>

_form_1


<?php echo CHtml::ajaxSubmitButton(Yii::t('forms','Create'),CHtml::normalizeUrl(array('doy/addnew')),array('replace'=>'#doy','type'=>'POST'),array(

                //test

                )); ?>



and worked like it should echoing test.

No other issues related to how the "… i dont want only test to be rendered but in fact i want a new dropdownlist with the values submitted previously to be the selected. Please help because i am new to ajax and yii." as described in topic

I suggest you read this cookbook article

http://www.yiiframework.com/doc/cookbook/24/

It describes how to update a dropdown already present in the page.

You also may want to read this thread.

/Tommy

Yes i have seen this before (cookbook dependent dropdownlist) The problem is that the dropdownlist does not exist any more because the <div> containing the list has been update it with 1st ajaxLink.

The solution i am thing is that the dropdownlist will be hidden and after the ajaxSubmit call (2nd) will be feed with data and appeared again.

Any other suggestions or corrections welcome.