Yii Framework Forum: CHtml::textField and ajax - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CHtml::textField and ajax Rate Topic: -----

#1 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 185
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 30 January 2010 - 05:28 AM

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";
	$this->renderPartial('town',$data,false,true);
    }


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";


with

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


nothing appears at all.

Please help.
Thank you.
0

#2 User is offline   nohero 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-January 10

Posted 06 February 2010 - 02:30 PM

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' => '???'
                )));
</div>



<div id="nick_result">

</div>


In my Controller I want:
public function actionCheckNick() {
    echo "Nick: ".$_POST["username"];
}

but nothing else than "Nick: " appears...
0

#3 User is offline   scythah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 20-April 09

Posted 06 February 2010 - 02:50 PM

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

#4 User is offline   nohero 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-January 10

Posted 06 February 2010 - 03:22 PM

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',
                'ajax'=>array(
                    'type'=>'POST',
                    'url'=>$url,
                    'update'=>'#nick_result'
                )
               )); ?>
    </div>
    <div id="nick_result">

    </div>


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)

hth
nohero
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users