ymuskie
(Muskie88)
October 11, 2010, 4:51pm
1
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!
viter
(Chekhiv)
October 12, 2010, 7:22am
2
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.
ymuskie
(Muskie88)
October 12, 2010, 10:30am
3
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!
viter
(Chekhiv)
October 12, 2010, 10:52am
4
Good tool for tracing ajax problems is firebug (firefox extension).
Do you use it? It helps me a lot.
zaccaria
(Matteo Falsitta)
October 12, 2010, 12:59pm
5
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.
ymuskie
(Muskie88)
October 12, 2010, 1:51pm
6
viter,
I did use firebug. When I create a new record, I could see
it triggered the "transaction/temp" and return "12345".
it called the create.php
zaccaria,
I’ll try your suggestion and see what’s going on.
Thanks!
mdomba
(Maurizio Domba Cerin)
October 12, 2010, 2:04pm
7
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
viter
(Chekhiv)
October 12, 2010, 2:11pm
8
What if you replace
'update'=>'#Transaction_share_qty'
with
'success'=>'js:function(data)
{
$("#Transaction_share_qty").val(data);
}
ymuskie
(Muskie88)
October 13, 2010, 1:56am
9
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);
}
viter
(Chekhiv)
October 13, 2010, 5:56am
10
ymuskie:
But, one more question, it seems I couldn’t use:
CHtml::activeId($model,'share_qty') to replace the "Transaction_share_qty"
You can try this
'success'=>'js:function(data)
{
$("#'.CHtml::activeId($model,'share_qty').'").val(data);
}'
ymuskie
(Muskie88)
October 13, 2010, 10:50am
11
viter,
Amazing. Solved!
I initially using the "\" to escape the single quote. That caused the problem.
Thanks again and again!