Yii Framework Forum: updating form fields with ajax? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

updating form fields with ajax? Rate Topic: -----

#1 User is offline   Russell England 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 10-March 11
  • Location:Stourbridge, UK

Posted 18 March 2011 - 08:42 AM

How do I update a form field with ajax?

I'm following the example at:
http://www.yiiframework.com/wiki/49/


In my form I have

vehicle/_form.php
<div id="data">
   <?php echo 'before ajax'; // $this->renderPartial('_ajaxContent', array('myValue'=>$myValue)); ?>
</div>

<?php echo CHtml::ajaxButton ("Update data",
                              CController::createUrl('vehicle/UpdateAjax'), 
                              array('update' => '#data'));
?>


The above works and updates the div data as expected. But if I use
                              array('update' => '#Vehicle_chassis_number'));


Nothing happens... Changing update to replace removes the input box and displays the text.

                              array('replace' => '#Vehicle_chassis_number'));


I just need to update the input value, is there an easier what to do this?

Actually, what I need to do is update several fields with values based on a car registration lookup using SOAP.




The other files are the same as the example
views/vehicle/_ajaxContent.php
<?php echo $mymodel ?>


controllers/VehicleController.php
    public function actionUpdateAjax()
    {
        $data = array();
        $data["myValue"] = "Content updated in AJAX";
 
        $this->renderPartial('_ajaxContent', $data, false, true);
    }

0

#2 User is offline   Russell England 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 10-March 11
  • Location:Stourbridge, UK

Posted 18 March 2011 - 09:14 AM

Woohoo!!!

Following this example
http://www.yiiframework.com/wiki/24/

Figured out I needed to use this code instead and don't need the _ajaxContent.php file

    public function actionUpdateAjax()
    {
		
		echo CHtml::tag('input',		
		array('value'=>'Content updated in AJAX'));
    }


so... next question is... how do I update multiple fields?
0

#3 User is offline   Russell England 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 10-March 11
  • Location:Stourbridge, UK

Posted 18 March 2011 - 09:20 AM

Nope... that didn't work, it was a replace so just replaced it with an input box rather than just updating the value.

Still stuck...
0

#4 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 18 March 2011 - 09:55 AM

Follow this guide for ajax-driven form.
0

#5 User is offline   Russell England 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 10-March 11
  • Location:Stourbridge, UK

Posted 21 March 2011 - 05:47 AM

Thank you Zaccaria, that's a very useful piece of code for creating new lookups.

But I'm still not sure how to update the other fields on my form?

Thanks, Russ
0

#6 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 21 March 2011 - 06:19 AM

You can return the json as you need, and then update the field using the id.

For know what is the id of an input field, use CHtml::activeId($model, 'attribute').

Anyway I never use this approach, too much stuffs on wich to think. I prefer keep the form simple and submit the entire form via ajax.
0

#7 User is offline   Russell England 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 10-March 11
  • Location:Stourbridge, UK

Posted 21 March 2011 - 07:16 AM

oh I'd prefer a much simpler solution, do you have a model/view/controller example?
0

#8 User is offline   Russell England 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 10-March 11
  • Location:Stourbridge, UK

Posted 21 March 2011 - 12:28 PM

Finally got it to work based on the link you gave and using json

views/vehicle/_form.php
        ...
	<div class="row">
		<?php echo $form->labelEx($model,'registration_number'); ?>
		<?php echo $form->textField($model,'registration_number',array('size'=>8,'maxlength'=>8)); ?>
		<?php echo $form->error($model,'registration_number'); ?>
	</div>
	
	<div class="row">
		<?php echo $form->labelEx($model,'chassis_number'); ?>
		<?php echo $form->textField($model,'chassis_number',array('size'=>17,'maxlength'=>17)); ?>
		<?php echo $form->error($model,'chassis_number'); ?>
	</div>	
	
	<?php echo CHtml::button('HPI Check', array( 'onclick'=>"{hpiCheck();}" ) ); ?>

	<div id='hpistatus'></div>
	<script type="text/javascript">
	function hpiCheck()
	{
		<?php echo CHtml::ajax(array(
				'url'=>CController::createUrl('vehicle/hpiCheck'),
				'data'=>array('registration_number'=>'js:$(\'#Vehicle_registration_number\').val()', 
								'chassis_number'=>'js:$(\'#Vehicle_chassis_number\').val()'),
				'type'=>'post',
				'dataType'=>'json',
				'success'=>"function(data)
				{
					$('#hpiStatus').html(data.status);
					$('#Vehicle_trim').val(data.trim);
					$('#Vehicle_chassis_number').val(data.chassis_number);
					$('#Vehicle_registration_number').val(data.registration_number);
                                        // .... list of fields to update
				} ",
				))?>;
		return false;  
	} 
	</script>
        ...


controllers/VehicleController.php (also add 'hpicheck' to access rules)
	public function actionHpiCheck()
	{
		if (Yii::app()->request->isAjaxRequest)
		{
			
			$chassis_number = Yii::app()->request->getParam( 'chassis_number' );
			$registration_number = Yii::app()->request->getParam( 'registration_number' );
			
			// Put SOAP logic here to get vehicle details from HPI check
			
			if (($chassis_number != '') || ($registration_number != ''))
			{
					echo CJSON::encode(array(
						'error'=>'false',
						'status'=>'HPI check complete', 
						'trim'=>$trim;
						'chassis_number'=>$chassis_number,
						));
					// exit;               
					Yii::app()->end();
				
			}
			else
			{
				echo CJSON::encode(array(
					'error'=>'true',
					'status'=>'HPI check failed, please enter a registration number or chassis number '
					));
				// exit;               
				Yii::app()->end();
			}
		}
	}

0

#9 User is offline   BIG FISH 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 12-May 12

Posted 27 May 2012 - 12:55 AM

View Postzaccaria, on 21 March 2011 - 06:19 AM, said:

You can return the json as you need, and then update the field using the id.

For know what is the id of an input field, use CHtml::activeId($model, 'attribute').

Anyway I never use this approach, too much stuffs on wich to think. I prefer keep the form simple and submit the entire form via ajax.



hey can you help me with an example that how to return with Json from controller and use it in view!!
Thanks in advanced..
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users