Simple Ajax but failed

For this testing, I’m expecting the “12345” show up in the text field of share_qty under either CREATE or UPDATE the record.

But after spent 3 days, I still can’t make it work.

_form.php - (Just extracted portion of the code)




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

	'id'=>'transaction-form',

	'enableAjaxValidation'=>true,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


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

	<div class="row">

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

		<?php echo $form->textField($model,'stock_id',array('ajax' => array(								                     'type'=>'POST', 														'url'=>CController::createUrl('transaction/temp'),

'update'=>'#Transaction_share_qty',

		); ?>

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

	</div>




	<div class="row">

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

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

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

	</div>



TransactionController.php - (Just extracted portion of the code)




class TransactionController extends Controller

{

	/**

	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

	 * using two-column layout. See 'protected/views/layouts/column2.php'.

	 */


	public function actionTemp() {

		echo '12345';

	}

	

	public function accessRules()

	{

		return array(

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

						'actions'=>array('create','update','temp'),

						'users'=>array('@'),

					),

		);

	}

}



Thanks!

You have syntax error in your _form.php:

In line


<?php echo $form->textField($model,'stock_id',array('ajax' => array(                                                                                 'type'=>'POST',                                                                                                            'url'=>CController::createUrl('transaction/temp'),

'update'=>'#Transaction_share_qty',

                ); ?>

You missed two ‘)’ in the end of the line.

Must be


<?php echo $form->textField($model,'stock_id',array('ajax' => array(                                                                                 'type'=>'POST',                                                                                                            'url'=>CController::createUrl('transaction/temp'),

'update'=>'#Transaction_share_qty',

                ))); ?>

And also instead of


'update'=>'#Transaction_share_qty'

I’d use


'update'=>CHtml::activeId($model,'share_qty')

to be sure that you use the right id.

Viter,

Thanks for the prompt reply.

The two ‘)’ you mentioned are under my original code. I believe I deleted them accidentally while I try to tidy up the code in this post. Any other thought about the problem?

And, I also use the at the beginning:




'update'=>CHtml::activeId($model,'share_qty')



I hard coded the ID there is for another testing purpose.

Thanks!

Good tool for tracing ajax problems is firebug (firefox extension).

Do you use it? It helps me a lot.

Are you sure that the textbox is really sending the ajax request?

Maybe you should do something like:

‘onChange’/‘onBlur’=>CHtml::ajax(…)

Also take in consideration the good advice of viter, use some tool for track the javascript, and your life will never been the same.

viter,

I did use firebug. When I create a new record, I could see

  1. it triggered the "transaction/temp" and return "12345".

  2. it called the create.php

zaccaria,

I’ll try your suggestion and see what’s going on.

Thanks!

The

‘update’=>’#Transaction_share_qty’,

says that the content of the DOM element with #Transaction_share_qty should be replaced with the result of the ajax call (in your case 12345)

It will not update the INPUT of the textfield… .for that you would need a bit of jQuery

What if you replace




'update'=>'#Transaction_share_qty'

with


'success'=>'js:function(data)

{

   $("#Transaction_share_qty").val(data);

}

mdomba,

Thanks for the explanation.

viter,

Your code works!!!

But, one more question, it seems I couldn’t use:




CHtml::activeId($model,'share_qty') to replace the "Transaction_share_qty"



for this code?




'success'=>'js:function(data)

{

   $("#Transaction_share_qty").val(data);

}



You can try this


'success'=>'js:function(data)

{

   $("#'.CHtml::activeId($model,'share_qty').'").val(data);

}'

viter,

Amazing. Solved!

I initially using the "\" to escape the single quote. That caused the problem.

Thanks again and again!