CHtml::textField and ajax

Hello everyone.

I have a problem with ajax in CHtml::textField.

In my view I have

echo CHtml::textField("input_town",'',array('id'=>'input_town','ajax' => array(

                        'type' =>'POST',

                        'url' => CController::createUrl('town'),

                        'update' => '#townlist',

                        'data' => 'html'


And in my controller

public function actionTown()


        $data = array();

        $data["myValue"] = "hello";



and also view town.php with

<?php echo $myValue; ?>

My problem is: when I type in the text field onchange event does not work, but "hello" appears only when I click outside my input field.

Another problem: how can I pass to the controller the value of my input field?

When I replace

$data["myValue"] = "hello";


$data["myValue"] = $_POST['input_town'];

nothing appears at all.

Please help.

Thank you.

Hi I have nearly the same problem here, would appreciate any help!

I don’t know how to actually send the input of the TextField to my controller :(

This code calls my Controller correctly but I don’t get the input Text…

So what do I need to insert into the data field of the ajax-array?

I already tried to write a jQuery Script which extracts the input data and tried to post it to my

Controller, but it didn’t work =/

I extracted the data with $("#nick_field").serialize() thats exactly what I need but I need it in the activeTextField-ajax-array-thing now.

<div id="simple">

            <?php echo CHtml::activeLabel($registration,'username'); ?>

            <?php echo CHtml::activeTextField($registration,'username',array('id'=>'nick_field','ajax' => array(

                        'type' =>'POST',

                        'url' => CController::createUrl('site/checkNick'),

                        'update' => '#nick_result',

                        'data' => '<img src='' class='bbc_emoticon' alt='???' />'



<div id="nick_result">


In my Controller I want:

public function actionCheckNick() {

    echo "Nick: ".$_POST["username"];


but nothing else than "Nick: " appears…

The onChange event only occurs when you select away from the textfield. What you are probably wanting to use is onKeyPress

Hi folks!

Just wanted to post the solution for my Problem:

What the code does:

If the Textfield is blurred (loses focus) the checkNick function in the Controller is

called, where the Input of the Textfield will be checked.

In my View where the Form is:

<div id="simple">

            <?php echo CHtml::activeLabel($registration,'username'); ?>

            <?php echo CHtml::activeTextField($registration,'username',array('id'=>'nick_field',






               )); ?>


    <div id="nick_result">


In my Controller I can access the posted Data with:

public function actionCheckNick() {

        $regForm = $_POST["RegistrationForm"];

        $user = $regForm["username"];

        echo $user;


(RegistrationForm is the class/model of my Form, Yii seems to automatically post the whole formular with the code I wrote, I always thought only the activeTextField is posted)