Yii Framework Forum: Ajax - Change 4 Fields with one AJAX Request.. - Yii Framework Forum

Jump to content

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

Ajax - Change 4 Fields with one AJAX Request.. Rate Topic: -----

#1 User is offline   Andreas123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 11-March 11
  • Location:$3RdF

Posted 15 March 2011 - 04:28 PM

Hi Guys,

I have a dropDown field with AJAX code..
Once a user changes selection I want to update the content of 4 different TextFields.
How do I do that? Right now I can only update one textField.

THX

Here is the form snippet:


		<?php echo $form->label($model,'country'); ?>
		<?php 
			echo CHtml::activedropDownList($model,'country',$model->getCountryOptions(),
				array(
					'ajax' => array(
						'type'=>'POST', //request type
						'url'=>CController::createUrl('user/DynamicStates'), 
						//Style: CController::createUrl('currentController/methodToCall')
						'update'=>'#text1',
						//'data'=>'js:javascript statement' 
						//leave out the data key to pass all form values through
					)
				)
			);
		?>


                <?php echo CHtml::textField('text1', 'some value1'); ?>
                <?php echo CHtml::textField('text2', 'some value2'); ?>
                <?php echo CHtml::textField('text3', 'some value3'); ?>
                <?php echo CHtml::textField('text4', 'some value4'); ?>



0

#2 User is offline   Anupam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 28-February 10
  • Location:INDB, India

Posted 15 March 2011 - 08:09 PM

U should return JSON encoded data from the controller action.
checkout CJSON::encode or CJavaScript::jsonEncode for that.

Then u can put the values to text fields as below.
try this:
echo CHtml::activedropDownList($model,'country',$model->getCountryOptions(),
array(
	'ajax' => array(
	'type'=>'POST', //request type
	'dataType'=>'json',
	'url'=>CController::createUrl('user/DynamicStates'),
	'success'=>'function(data){
		$("#text1").text(data.text1);
		$("#text2").text(data.text2);
		$("#text3").text(data.text3);
		$("#text4").text(data.text4);
		}')
	)
);

0

#3 User is offline   Andreas123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 11-March 11
  • Location:$3RdF

Posted 16 March 2011 - 12:30 PM

Yes! THX that works!
0

#4 User is offline   Anupam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 28-February 10
  • Location:INDB, India

Posted 16 March 2011 - 06:04 PM

View PostAndreas123, on 16 March 2011 - 12:30 PM, said:

Yes! THX that works!


welcome, anytime! :)
0

#5 User is offline   kaiser971 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 08-April 11

Posted 06 May 2011 - 03:24 PM

Hi andreas123
Can you put your controller code please? i have some difficulty with CJSON in my controller.
thanks you

P.S:
sorry if i make some mistake ^^ i'm French
0

#6 User is offline   kuanfai 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 13-October 10

Posted 18 July 2011 - 10:13 PM

I have the same logic in my form but I can't get the 4 text fields updated with the json object.

In my controller
public function actionLoadPubConfig() {
//...codes to set the variables below

        echo CJSON::encode(array(
            'adminname'=>$adminname,
            'adminemail'=>$adminemail,
            'csemail'=>$csemail,
            'aooemail'=>$aooemail
}


and my form:
<?php echo EHtml::activeDropDownList($model, 'pub', $model->pub_list,
                        array(
                            'ajax' => array(
                                'type' => 'POST',
                                'dataType'=> 'json',
                                'url' => CController::createUrl('loadPubConfig'),
                                'success'=>'function(data){
                                     $("#AddEventForm_adminname").text(data.adminname);
                                     $("#AddEventForm_adminemail").text(data.adminemail);
                                     $("#AddEventForm_csemail").text(data.csemail);
                                     $("#AddEventForm_aooemail").text(data.aooemail);
                                }',
                            )
                        ));?>


the first time when i change the value of pub field, the string will be output to the page, but the text fileds are not updated:

Quote

{"adminname":"admin","adminemail":"admin@myaddress.com.sg","csemail":"cs@myaddress.com.sg","aooemail":"admin_aoo@myaddress.com.sg"}



after the second time i change the pub field, the string on the page is not update, neither the 4 text fields. but there is an error message in jquery:

Quote


Uncaught Syntax error, unrecognized expression: {"adminname":"admin_epu_st"




please help! thanks.
0

#7 User is offline   kuanfai 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 13-October 10

Posted 19 July 2011 - 02:57 AM

how come in my case the success function was not executed at all?
0

#8 User is offline   kuanfai 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 13-October 10

Posted 19 July 2011 - 04:34 AM

solved, found there is another place to call the ajax function.
0

#9 User is offline   Tim Turnquist 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 29-August 11
  • Location:Chanhassen, MN, US of A

Posted 05 October 2011 - 02:08 PM

Sorry to grab this old post, but it is exactly what I was looking for -- and I can't quite get it to work right...

I used Gii to set up my model so I did not write some of this*, but here is what I have:
views/NC/pobased/_form.php contains

...
	<div class="row">
		<?php 
                echo $form->labelEx($model,'PO_NUM'); ?>
		<?php echo $form->textField($model, 'PO_NUM'
                        , array(
                            'ajax'=>
                                array('type'=>'GET'
                                 , 'url'=>CController::createUrl( $this->id ."/changeStuff")
                                 , 'dataType'=>'json'
                                 , 'success'=>'function( myData ) {
                                     $("#' . CHtml::activeId( $model, 'TOTAL_SIZE_LOT') .'" ).text(myData.tot_size_lot);
                                     $("#' . CHtml::activeId( $model, 'TOTAL_SIZE_LOT') .'" ).refresh;
                                     $("#' . CHtml::activeId( $model, 'VENDOR') .'" ).text(myData.vendor);
                                     $("#' . CHtml::activeId( $model, 'VENDOR_NC_HANDLER') .'" ).text(myData.ven_nc_handler);
                                     var o = $("#' . CHtml::activeId( $model, 'PART_NO') .'" );
                                     for( var i in myData.part_nos ){
                                         o.append($(\'<option></option>\').val(myData.part_nos[i]["ID"]).html(myData.part_nos[i]["NAME"]));
                                     }
                               }'
                     )   ) ); ?>
		<?php echo $form->error($model,'PO_NUM'); ?>
	</div>


and my PoBasedController is an instance of controllers/NC/HeadController.php which contains:
        public function actionChangeStuff(){
         $sql = <<<EOD
SELECT POLA.PART_NO as id
        , CONCAT( CONCAT( POLA.PART_NO , ' - ' ), IP.DESCRIPTION )AS name
FROM IFSAPP.PURCHASE_ORDER_LINE_ALL POLA JOIN IFSAPP.INVENTORY_PART IP ON POLA.PART_NO = IP.PART_NO
WHERE POLA.ORDER_NO = '{$_REQUEST['Pobased']['PO_NUM']}'
EOD;
         $connection = Yii::app()->db;
         $command = $connection->createCommand($sql);
         $data = $command->queryAll();
         $ret= array(
                'tot_size_lot' => 15,
                'vendor'        => "bob",
                'ven_nc_handler' =>'monkeyboy',
               'part_nos' => $data );

         echo json_encode( $ret );

     }


[OK, I confess that I have not finished getting the data from the DB for this, but that is easy.]

What happens when I run this in a browser is that none of my TextFields are updated, but my DropDownList works like a charm! Using Google Chrome's Developer tools to step through the Javascript it shows that the TextField.textContent changes as expected to the values that I want it to, but they never "refresh" and show the data on the HTML page. What am I doing wrong?

Please advise.

Thanks,
Tim

*I am using an Oracle DB which uses all capitals for column and table names, so I when Gii set it up I was able to change my class names, but not my field/attribute names. I intend to clean this up, since it is soooo ugly to read, but I need it working first.

This post has been edited by Tim Turnquist: 06 October 2011 - 05:53 AM

Thanks,
<Tim><


Ubuntu 10.04
Apache2
PHP5
Oracle 10 & XE
Yii-1.1.8r3324
0

#10 User is offline   Tim Turnquist 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 29-August 11
  • Location:Chanhassen, MN, US of A

Posted 07 October 2011 - 12:55 AM

Just in case anyone finds this thread, I found my error. I used the JQuery function text() instead of val(). My code should have been
        <div class="row">
                <?php 
                echo $form->labelEx($model,'PO_NUM'); ?>
                <?php echo $form->textField($model, 'PO_NUM'
                        , array(
                            'ajax'=>
                                array('type'=>'GET'
                                 , 'url'=>CController::createUrl( $this->id ."/changeStuff")
                                 , 'dataType'=>'json'
                                 , 'success'=>'function( myData ) {
                                     $("#' . CHtml::activeId( $model, 'TOTAL_SIZE_LOT') .'" ).val(myData.tot_size_lot);
                                     $("#' . CHtml::activeId( $model, 'VENDOR') .'" ).val(myData.vendor);
                                     $("#' . CHtml::activeId( $model, 'VENDOR_NC_HANDLER') .'" ).val(myData.ven_nc_handler);
                                     var o = $("#' . CHtml::activeId( $model, 'PART_NO') .'" );
                                     for( var i in myData.part_nos ){
                                         o.append($(\'<option></option>\').val(myData.part_nos[i]["ID"]).html(myData.part_nos[i]["NAME"]));
                                     }
                               }'
                     )   ) ); ?>
                <?php echo $form->error($model,'PO_NUM'); ?>
        </div>


I found it helpful to actually read the JQueryAPIon such things because it says [quite clearly] when to use text() and when to use val() [my emphasis added]:

Quote

The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.

Thanks,
<Tim><


Ubuntu 10.04
Apache2
PHP5
Oracle 10 & XE
Yii-1.1.8r3324
0

#11 User is offline   iqbalbaba 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-December 12

Posted 13 February 2013 - 04:37 AM

HI
BELOW is my code. when you type doc_name and Prefix and press tab key it should merge doc_name and Prefix textfields and display it in Preview field. Please help me.

<div class="row">
<?php echo $form->labelEx($model,'Doc_name'); ?>
<?php echo $form->textField($model,'Doc_name',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'Doc_name'); ?>
</div>

<div class="row"">
<?php echo $form->labelEx($model,'Prefix'); ?>
<?php echo $form->textField($model,'Prefix',array('size'=>10,'maxlength'=>10));?>
<?php echo $form->error($model,'Prefix'); ?>
</div>
<?php echo $form->labelEx($model,'Preview'); ?>

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

<?php echo $form->error($model,'Preview'); ?>
</div>

Please help me
0

#12 User is offline   iqbalbaba 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-December 12

Posted 13 February 2013 - 06:30 AM

i need a code for the above problem, please help me
0

Share this topic:


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

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