viter
(Chekhiv)
January 30, 2010, 10:28am
1
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.
nohero
(The Schweigi)
February 6, 2010, 7:30pm
2
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='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />'
)));
</div>
<div id="nick_result">
</div>
In my Controller I want:
public function actionCheckNick() {
echo "Nick: ".$_POST["username"];
}
but nothing else than "Nick: " appears…
scythah
(Scythah)
February 6, 2010, 7:50pm
3
The onChange event only occurs when you select away from the textfield. What you are probably wanting to use is onKeyPress
nohero
(The Schweigi)
February 6, 2010, 8:22pm
4
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