renderpartial with new values or asign values with ajax

Hi,

i have a form with one dependent dropdown and a autocomplete field, both depending on a dropdown.

this is my _form.php




<div class="form">


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

	'id'=>'avize-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Campurile marcate cu <span class="required">*</span> sunt obligatorii.</p>


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

    

    <div class="row">

		<?php

			$models = Facturi::model()->findAll(array('order' => 'id_factura DESC'));

			$list	= array();

			$list	= CHtml::listData($models,'id_factura','SerieNumar');

			$list	= array('0' => 'Neasociat', '-1' => 'Urmeaza a se factura') + $list;

			$model->id_cumparator = 100;

		?>

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

        <?php echo $form->dropDownList($model,'id_factura',$list,

				array(

				'ajax' => array(

					'type'=>'POST', //request type

					'url'=>CController::createUrl('avize/setidfactura'), // url to call.

					'update'=>'#cu_factura', //selector to update

					'data'=>array('Avize[id_factura]'=>'js:this.value'),

					//leave out the data key to pass all form values through

				))

		); ?>

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

	</div>


    <div class="row">

		<?php

			$models	= Furnizori::model()->findAll(array('order' => 'nume_furnizor'));	// retrieve the models from db

			$list	= CHtml::listData($models,'id_furnizor','nume_furnizor');			// format models as $key=>$value with listData

			

			$id_client = 0;

		?>

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

		<?php echo $form->dropDownList($model,'id_furnizor',$list); ?>

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

	</div>

    

    <div id="cu_factura">

    <div class="row">

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

        <?php

        	echo $form->dropDownList($model,'tip_cumparator',$model->TipCumparator,

				array(

				'ajax' => array(

					'type'=>'POST', //request type

					'url'=>CController::createUrl('avize/settipclient'), // url to call.

					'update'=>'#data', //selector to update

					'data'=>array('Avize[tip_cumparator]'=>'js:this.value'),

					//leave out the data key to pass all form values through

				))

			);

		?>

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

	</div>


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

	<div class="row" id="data">

		<?php

			if(isset($model->tip_cumparator) && !empty($model->id_cumparator)) {

				if($model->tip_cumparator == 1) {

					Pj::model()->nume_pj = Pj::model()->findByPk($model->id_cumparator)->nume_pj;	// Completeaza campul

					$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

						'name' => 'Pj[nume_pj]',

						'sourceUrl' => array('pj/autocompleteTest'),

						'value' =>  (Pj::model()->nume_pj) ? Pj::model()->nume_pj: Pj::model()->nume_pj,

							'options' => array(

								'showAnim' => 'fold',

								'select' => 'js:function(event, ui){ jQuery("#Avize_id_cumparator").val(ui.item["id"]); }'

							),

							'htmlOptions' => array(

								'style'=>'width: 200px;',

							),

					));

				} elseif($model->tip_cumparator == 2) {

					Pf::model()->nume_pf = Pf::model()->findByPk($model->id_cumparator)->nume_pf;	// Completeaza campul

					$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

						'name' => 'Pf[nume_pf]',

						'sourceUrl' => array('pf/autocompleteTest'),

						'value' =>  (Pf::model()->nume_pf) ? Pf::model()->nume_pf: Pf::model()->nume_pf,

							'options' => array(

								'showAnim' => 'fold',

								'select' => 'js:function(event, ui){ jQuery("#Avize_id_cumparator").val(ui.item["id"]); }'

							),

							'htmlOptions' => array(

								'style'=>'width: 200px;',

							),

					));

				};

			};

		?>

    </div>

    	<?php echo $form->hiddenField($model,'id_cumparator'); ?>

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

    </div> <!-- EOF CU FACTURA -->


	<div class="row">

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

		<?php echo $form->dropDownList($model,'serie_aviz',Diverse::items('SerieAviz'),

				array(

				'ajax' => array(

					'type'=>'POST', //request type

					'url'=>CController::createUrl('avize/dynamicnumaraviz'), // url to call.

					'success'=>'js:function(data){$("#Avize_nr_aviz").val(data);}',

					'data'=>array('Avize[serie_aviz]'=>'js:this.value'),

					'dataType'=>'json',

				)

			)); ?>

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

	</div>


    <div class="row">

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

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

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

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php 

	// pentru incarcarea automata a axajurilor

	//Yii::app()->clientScript->registerScript('triggerChangeOnADropDown', '$(window).load(function(event){$("#id_cumparator").change();});');

	if(!isset($model->nr_aviz)){

		Yii::app()->clientScript->registerScript('triggerChangeOnADropDown', '$(window).load(function(event){$("#Avize_serie_aviz").change();});');

	}

?>


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


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



this is from controler.php




public function actionSetIdFactura(){

        $data = array();

        $data["ajax_id_factura"] = $_POST['Avize']['id_factura'];

 

        $this->renderPartial('_ajaxContent', $data, false, true);

	}



and this is the _ajaxContent.php




<?php

if(isset($ajax_id_factura)){

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

			'id'=>'avize-form',

			'enableAjaxValidation'=>false,

		));

		$model					=new Avize;

	

	if($ajax_id_factura > 0){


		$get_factura = Facturi::model()->findByPk((int)$ajax_id_factura);

		

		$model->tip_cumparator = $get_factura->tip_cumparator;

		$model->id_cumparator = $get_factura->id_cumparator;

?>

        

		<div class="row">

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

        <?php

        	echo $form->dropDownList($model,'tip_cumparator',$model->TipCumparator,

				array(

				'ajax' => array(

					'type'=>'POST', //request type

					'url'=>CController::createUrl('avize/settipclient'), // url to call.

					'update'=>'#data', //selector to update

					'data'=>array('Avize[tip_cumparator]'=>'js:this.value'),

					//leave out the data key to pass all form values through

				))

			);

		?>

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

	</div>


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

	<div class="row" id="data">

		<?php

			if(isset($model->tip_cumparator) && !empty($model->id_cumparator)) {

				if($model->tip_cumparator == 1) {

					Pj::model()->nume_pj = Pj::model()->findByPk($model->id_cumparator)->nume_pj;	// Completeaza campul

					$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

						'name' => 'Pj[nume_pj]',

						'sourceUrl' => array('pj/autocompleteTest'),

						'value' =>  (Pj::model()->nume_pj) ? Pj::model()->nume_pj: Pj::model()->nume_pj,

							'options' => array(

								'showAnim' => 'fold',

								'select' => 'js:function(event, ui){ jQuery("#Avize_id_cumparator").val(ui.item["id"]); }'

							),

							'htmlOptions' => array(

								'style'=>'width: 200px;',

							),

					));

				} elseif($model->tip_cumparator == 2) {

					Pf::model()->nume_pf = Pf::model()->findByPk($model->id_cumparator)->nume_pf;	// Completeaza campul

					$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

						'name' => 'Pf[nume_pf]',

						'sourceUrl' => array('pf/autocompleteTest'),

						'value' =>  (Pf::model()->nume_pf) ? Pf::model()->nume_pf: Pf::model()->nume_pf,

							'options' => array(

								'showAnim' => 'fold',

								'select' => 'js:function(event, ui){ jQuery("#Avize_id_cumparator").val(ui.item["id"]); }'

							),

							'htmlOptions' => array(

								'style'=>'width: 200px;',

							),

					));

				};

			};

		?>

    </div>

		<?php echo $form->hiddenField($model,'id_cumparator'); ?>

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

<?php

	}else{

?>

		<div class="row">

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

        <?php

        	echo $form->dropDownList($model,'tip_cumparator',$model->TipCumparator,

				array(

				'ajax' => array(

					'type'=>'POST', //request type

					'url'=>CController::createUrl('avize/settipclient'), // url to call.

					'update'=>'#data', //selector to update

					'data'=>array('Avize[tip_cumparator]'=>'js:this.value'),

					//leave out the data key to pass all form values through

				))

			);

		?>

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

	</div>


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

	<div class="row" id="data">

		<?php

			if(isset($model->tip_cumparator) && !empty($model->id_cumparator)) {

				if($model->tip_cumparator == 1) {

					Pj::model()->nume_pj = Pj::model()->findByPk($model->id_cumparator)->nume_pj;	// Completeaza campul

					$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

						'name' => 'Pj[nume_pj]',

						'sourceUrl' => array('pj/autocompleteTest'),

						'value' =>  (Pj::model()->nume_pj) ? Pj::model()->nume_pj: Pj::model()->nume_pj,

							'options' => array(

								'showAnim' => 'fold',

								'select' => 'js:function(event, ui){ jQuery("#Avize_id_cumparator").val(ui.item["id"]); }'

							),

							'htmlOptions' => array(

								'style'=>'width: 200px;',

							),

					));

				} elseif($model->tip_cumparator == 2) {

					Pf::model()->nume_pf = Pf::model()->findByPk($model->id_cumparator)->nume_pf;	// Completeaza campul

					$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

						'name' => 'Pf[nume_pf]',

						'sourceUrl' => array('pf/autocompleteTest'),

						'value' =>  (Pf::model()->nume_pf) ? Pf::model()->nume_pf: Pf::model()->nume_pf,

							'options' => array(

								'showAnim' => 'fold',

								'select' => 'js:function(event, ui){ jQuery("#Avize_id_cumparator").val(ui.item["id"]); }'

							),

							'htmlOptions' => array(

								'style'=>'width: 200px;',

							),

					));

				};

			};

		?>

    </div>

		<?php echo $form->hiddenField($model,'id_cumparator'); ?>

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

<?php

	}

		$this->endWidget();

};

?>



in browser everithing works perfect but when i subbmit the form

$model->tip_cumparator and $model->id_cumparator is not subbmited.

how do i see all the js values ?

i also tried to renderPartial the all _form with data chosen from the first dropdown selection and now it’s form inside form.

how do i do a new refresh or how do i solve the JS values to be sent when submited ?

Thans in advance

Try doing print_r($_POST) values and check if tip_cumparator and id_cumparator are being passed. It may also be that tip_cumparator and id_cumparator are not set to be safe for mass assignment, declare them as safe in the model rules.

For the form within the form it may be that you are updating the wrong div.

Try:




array(

   'ajax' => array(

     'type'=>'POST', //request type

     'url'=>CController::createUrl('avize/settipclient'), // url to call.

     'update'=>'#avize-form', //selector to update

     'data'=>array('Avize[tip_cumparator]'=>'js:this.value'),

)));



It looks to me that you are doing something like this:




<div id="formContainer">

  <div id="form">

  </div>

</div>



with your controller to rendering <div id="form"></div>, and your ajax also updating <div id="form"></div> it will result to something like this:




<div id="formContainer">

  <div id="form">

    <div id="form>

    </div>

  </div>

</div>



I hope that helps! Cheers!

Jaymard

thanks for your reply.

they are not sent via POST and i know it’s wrong with form inside form and i don’t know how to do it right.

I can see that your ajax script is actually over riding the POST data of your form. Try removing the data attribute of all you ajax script.




<?php echo $form->dropDownList($model,'serie_aviz',Diverse::items('SerieAviz'),

                                array(

                                'ajax' => array(

                                        'type'=>'POST', //request type

                                        'url'=>CController::createUrl('avize/dynamicnumaraviz'), // url to call.

                                        'success'=>'js:function(data){$("#Avize_nr_aviz").val(data);}',

                                        'data'=>array('Avize[serie_aviz]'=>'js:this.value'), <--- REMOVE THIS PART

                                        'dataType'=>'json', <-- AND REMOVE THIS PART

                                )

                        )); ?>



then do print_r($_POST) again

For the form within a form issue:

create a container div for your form in your initial view. And update that instead. Do something like this:




//_form

<div id="formContainer">

 <!-- PUT YOUR FORM HERE -->

</div>


//_ajaxContent

<!-- PUT YOUR FORM HERE -->


//add this to your ajax scripts

'ajax' => array(

'update'=>'#formContainer', <--- Add this

)