ymuskie
(Muskie88)
October 20, 2010, 3:40am
1
Hi,
Say I have a form with 3 fields - "id, name, total_score" plus some others.
Once user input the id, the name will auto prompted and total_score will also generated.
I just use ajax to do this work (pls let me know if this is the correct approach). So, setup the ajax under the id field and do whatever I want to. The problem is I don’t know how to get the value of field ‘id’ that user just entered so I can put it under the ajax’s ‘url’.
Any idea?
Thanks!
Spyros
(Spyros)
October 20, 2010, 4:32am
2
If your textfield id is "id" you have to do this:
<script type="text/javascript">
$("#id").blur(function(){
var id = $(this).val();
})
</script>
or if you don’t use blur
<script type="text/javascript">
var id = $("#id").val();
</script>
ymuskie
(Muskie88)
October 20, 2010, 5:11am
3
Spyros,
Thanks for the prompt replay.
My text field setting looks like this (XXX should replace the ‘id’ value that I’m looking for):
<?php echo $form->textField(
$model,
'id',
array(
'ajax'=> array(
'type'=>'POST',
'url'=>CController::createUrl('customer/xgetName&id='.XXX),
'success'=>'js:function(data) {
$("#'.CHtml::activeId($model,'name').'").val(data);
}'
)
)
);
It will set the "name" field to the value that is return from the function "actionXgetName($id)" in CustomerController.php.
How could I use your code?
Thanks!
zaccaria
(Matteo Falsitta)
October 20, 2010, 7:11am
4
You don’t need to add to the url, the ajax will post the form, so you will get the id in $POST[‘id’].
ymuskie
(Muskie88)
October 20, 2010, 9:23am
5
zaccaria,
I may explained not clearly.
User typed in the ‘id’, the ‘name’ and ‘total_score’ field will display a default value depends on what the ‘id’ is.
But, user will still need to put the value into ‘other fields’. So, the form will not be POSTed until all data filled in.
That’s why I need the ajax inside the ‘id’ field to help me to retrieve the ‘name’ and ‘total_score’.
Thanks!
zaccaria
(Matteo Falsitta)
October 20, 2010, 9:49am
6
You can get the id in post:
<?php echo $form->textField(
$model,
'id',
array(
'ajax'=> array(
'type'=>'GET',
'data'=>'js:{id:$(this).val()}',
'url'=>CController::createUrl('customer/xgetName),
'success'=>'js:function(data) {
$("#'.CHtml::activeId($model,'name').'").val(data);
}'
)
)
);
the line ‘data’=>‘js:{id:$(this).val()}’, will send the the content of the field in get, so in your action will work.
ram87
(C Ramkumar)
February 21, 2013, 8:41am
8
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php
echo $form->textField($model,'name',
array('ajax'=> array('type'=>'POST','url'=>CController::createUrl('site/AAjax'),
'success'=>'js:function(data) {
$("#'.CHtml::activeId($model,'empty').'").val(data);
}'
)
)
); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo CHtml::activeTextArea($model,'empty',array('rows'=>10, 'cols'=>100)); ?>
</div>
controller:
public function actionAAjax(){
echo " Good Morning Mr/Ms/Mrs. ";
echo $ram = $_POST[‘RamDbf’][‘name’];
//echo $_POST['name'];
//print_r($ram);
}