Form field value - ajax

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!

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>



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!

You don’t need to add to the url, the ajax will post the form, so you will get the id in $POST[‘id’].

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!

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.

Works like a charm!

Thanks!

<div class="row">

        &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'name'); ?&gt;


        &lt;?php 





        echo &#036;form-&gt;textField(&#036;model,'name',


        array('ajax'=&gt; array('type'=&gt;'POST','url'=&gt;CController::createUrl('site/AAjax'),


        'success'=&gt;'js:function(data) {


        &#036;(&quot;#'.CHtml::activeId(&#036;model,'empty').'&quot;).val(data);


        }'


        )


        )


        ); ?&gt;


        &lt;?php echo &#036;form-&gt;error(&#036;model,'name'); ?&gt;


        &lt;/div&gt;


        &lt;div class=&quot;row&quot;&gt;


        &lt;?php  echo CHtml::activeTextArea(&#036;model,'empty',array('rows'=&gt;10, 'cols'=&gt;100)); ?&gt;


        &lt;/div&gt;

controller:

public function actionAAjax(){

echo &quot; Good Morning Mr/Ms/Mrs.  &quot;;

echo $ram = $_POST[‘RamDbf’][‘name’];

 //echo &#036;_POST['name'];


//print_r(&#036;ram);


      


    }