Update texfield with ajax

Hi,

i’m trying to set a mail address from firstname et lastname of users.

For this, i use an ajax update.

The return field from the controller seems fine by me, but the update of the destination field didn’t work at all. I’ve tried to specify field directly (as it is represented in the source page), but nothing better…

My form view:




...

<div class="row">

	<?php echo $form->labelEx($model,'student_firstname'); ?>

	<?php echo $form->textField($model,'student_firstname',array('size'=>20,

	            'ajax'=>array(

			'type'=>'POST',

      			'url'=>CController::createUrl('student/adresseMailP7'),

//			'update'=> "#Student_univ_mail", 

			'update' => '#' . CHtml::activeId($model, 'univ_mail'),

		))); ?> 

	<?php echo $form->error($model,'student_firstname'); ?>

</div>

...

	<div class="row">

		<?php echo $form->labelEx($model,'univ_mail'); ?>

		<?php echo $form->textField($model,'univ_mail',array('size'=>40)); ?> 

		<?php echo $form->error($model,'univ_mail'); ?>

	</div>

...



My function in controller:




public function actionAdresseMailP7()

{

	$lastname = '';

	$firstname = '';

	if(isSet($_POST['Student']['student_lastname']))

	{

		$lastname = $_POST['Student']['student_lastname'];

	}

	if(isSet($_POST['Student']['student_firstname']))

	{

		$firstname = $_POST['Student']['student_firstname'];

	}

	if($lastname <> '' && $firstname <> '')

	{

		// mise en forme du prénom et du nom

		$firstname = trim($firstname);

		$firstname = strtolower($firstname);

	    $firstname = htmlentities($firstname, ENT_NOQUOTES, 'utf-8');

	    $firstname = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $firstname);

   		$firstname = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $firstname); // pour les ligatures e.g. '&oelig;'

   		$firstname = preg_replace('#&[^;]+;#', '', $firstname); // supprime les autres caractères

		$lastname = trim($lastname);

		$lastname = strtolower($lastname);

	    $lastname = htmlentities($lastname, ENT_NOQUOTES, 'utf-8');

	    $lastname = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $lastname);

   		$lastname = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $lastname); // pour les ligatures e.g. '&oelig;'

   		$lastname = preg_replace('#&[^;]+;#', '', $lastname); // supprime les autres caractères

   		$retour = $firstname . '.' . $lastname . '@toto.fr';

		echo CHtml::tag('input', array( 'type'=>'text' , 'value' => $retour, 'size'=>40));

	}

}



Did someone meet this issue?

If you check CHtml::ajax() source code, you’ll notice the ‘update’ property is used in a manner that is mainly intended for divs (and spans), because of the .html() method that can’t be applied to form inputs, I’m talking specifically about this:

So in your case, just use the ‘success’ property instead:


                    'ajax'=>array(

                        'type'=>'POST',

                        'url'=>CController::createUrl('student/adresseMailP7'),

//                      'update'=> "#Student_univ_mail", 

//                      'update' => '#' . CHtml::activeId($model, 'univ_mail'),

                        'success' => 'js:function (result) {

                                            $("#' . CHtml::activeId($model, 'univ_mail') . '").val(result);

                                      }'

                    )

Thanks a lot Bennouna, now it’s works!