Update Form Using Ajax

Hey there came with new issue. I have a form with ajax drop downs. while creating it works well but whenever i update the same the dropdowns are became empty (the following given image). What shoul be the problem ?

and even that the list is not completed …Pls help :-X

Ps. Any1 hav link pls forward it.

Thank u

JB

5758

problem.png

It’'s model validation rules issue

Please read this I hope it may be some help.

Ok this is my _form.php







<?php

/* @var $this IssueController */

/* @var $model Issue */

/* @var $form CActiveForm */

?>


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

	'id'=>'issue-form',

	'enableAjaxValidation'=>FALSE,

)); ?>


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


			<?php echo $form->labelEx($model,'Title',array('class'=>'control-label')); ?>

			<?php echo $form->textField($model,'Title',array('size'=>25,'maxlength'=>25)); ?>

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


    

			<?php echo $form->labelEx($model,'ProjectId',array('class'=>'control-label')); ?>

			<?php echo $form->DropDownList($model, 'ProjectId',CHtml::listData(Project::model()->findAll(), 'Id', 'Name'),

					array(

					'ajax' => array(

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

						'update'=>'#Issue_ProjectVersionId', //selector to be updated-drowdown to update

						'data'=>array('ProjectId'=>'js:this.value','YII_CSRF_TOKEN' => Yii::app()->request->csrfToken), //'data'=>'js:javascript st

																														//atement' any other data like CSRF tocken to be passed 

																														//if commentd all things will be passed 

						'url'=>CController::createUrl('ProjectVersion'), //url to call.i.e controller's-action to 

						),

					'empty'=>'Select Project'

				));?>

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


			<?php echo $form->labelEx($model,'ProjectVersion',array('class'=>'control-label')); ?>

			<?php echo $form->DropDownList($model, 'ProjectVersionId', array('value'=>isset($model->projectVersion->VersionName) ? $model->projectVersion->VersionName : "Select Version")); ?>

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





			<?php echo $form->labelEx($model,'BrowserId',array('class'=>'control-label')); ?>

			<?php echo $form->dropDownList($model,'BrowserId' , CHtml::listData(Browser::model()->findAll(), 'Id', 'Name'),

					array('ajax' => array(

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

							'update'=>'#Issue_BrowserVersionId', //selector to be updated-drowdown to update

							'data'=>array('BrowserId'=>'js:this.value','YII_CSRF_TOKEN' => Yii::app()->request->csrfToken), //'data'=>'js:javascript st

																															//atement' any other data like CSRF tocken to be passed 

																															//if commentd all things will be passed 

							'url'=>CController::createUrl('BrowserVersion'), //url to call.i.e controller's-action to 

							),

					 'empty'=>'Select Browser'

					)); ?>

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


			<?php echo $form->labelEx($model,'BrowserVersionId',array('class'=>'control-label')); ?>

			<?php echo $form->DropDownList($model, 'BrowserVersionId', array('value'=>isset($model->browserVersion->VersionName) ? $model->browserVersion->VersionName : "Select Version"),array()); ?>

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




			<?php echo $form->labelEx($model,'attachedfiles',array('class'=>'control-label')); ?>

			<?php 

    	      $this->widget('CMultiFileUpload', array(

        		     'model'=>$model,

		             //'name'=>'files',

		             'attribute'=>'attachedfiles',

		             'accept'=>'jpg|gif|png',

	            ));?>

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


	

		<div class="row buttons" style="margin-left: 160px">

			<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('class'=>'btn btn-primary')); ?>

			<?php echo CHtml::resetButton('Cancel', array('class'=>'btn btn-primary'));?>

		</div>


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




And The Controllers Actions





public function actionProjectVersion()

	{

		if ($_POST['ProjectId']!=='')

		{

		    $data=Projectversion::model()->findAll('ProjectId='.($_POST['ProjectId']));

			$data=CHtml::listData($data,'Id','VersionName');

		

		    foreach($data as $value=>$name)

		    {

		        echo CHtml::tag('option',

		                   array('value'=>$value),CHtml::encode($name),true);

		    }

		} else {

		 echo CHtml::tag('option',array('value'=>''),CHtml::encode('Select Version'),true);

		}

	}

	public function actionBrowserVersion()

	{

		if ($_POST['BrowserId']!=='')

		{

		    $data=Browserversion::model()->findAll('BrowserId='.($_POST['BrowserId'])); //if u remove data in _form.php then use ($_POST['Issue']['BrowserId']));

			$data=CHtml::listData($data,'Id','VersionName');

		

		    foreach($data as $value=>$name)

		    {

		        echo CHtml::tag('option',

		                   array('value'=>$value),CHtml::encode($name),true);

		    }

		}

		else {

			    echo CHtml::tag('option',array('value'=>''),CHtml::encode('Select Version'),true);

		}

	}




Create and Update actions are as it is …

:o[size=“6”][font=“Impact”]Still No answer … …[/font][/size] :o :o

You’re creating the drop down list wrong here:




<?php echo $form->DropDownList($model, 'ProjectVersionId', array(

    'value'=>isset($model->projectVersion->VersionName)

            ? $model->projectVersion->VersionName

            : "Select Version")); ?>



Look at the API if you’re unsure how to use a method; it has a pretty good search facility.

Check the correct parameters to provide for CActiveForm::dropDownList() here.

First Problem:

The third parameter is supposed to be the array used to populate the drop down list options. This should be an array in the following format:




array(

    'value1'=>'Display Name 1',

    'value2'=>'Display Name 2',

    ...

)



in your case, each value will be a ProjectVersionId and each display name will be the name of that project version.

This is often generated using the CHtml::listData() method.

Second Problem:

The CActiveForm will assign the value automatically, you do not need to do this yourself. If you want to show a prompt, use this form:




$form->dropDownList($model, 'AttrName', $options, array('prompt'=>'Select Version'));



The points above also apply to your browser version drop down list.

I suggest you pay close attention to the API as you try to unpick these problems.

Thank you for taking me to right. n sorry I didn’t get your this point. I mean how it will possible can u pls do the favor.

I tried bt whenever i click on update, it displays "Select Version"(for projectversion)

Done for Updating the existing. But Now the problem is creating new . cos for new the projectId in null.

Thank you my friend i’ve done it. bt i dont knw whether it is right way or not


<?php echo $form->DropDownList($model, 'ProjectVersionId',$model->isNewRecord ? array('prompt'=>'Select Version'):CHtml::listData(Projectversion::model()->findAll('ProjectId='.$model->ProjectId),'Id','VersionName')); ?>

If any, pls draw me to right way …