Checkbox Controller - How To Auto Populate The Forms Billing Address From Postal Address?

Hi

Newbie to Yii and looking for someone to advise how the following would be handled in the controller?

having already searched the forum but could not find the solution.

I have a single view which contains both a delivery address & a billing address.

I would like the user to be able to select a checkbox which would then auto populate all the billing fields with the same information as the delivery address.

If the user un-checks the checkbox then the billing address reverts back to been empty.

This is a sample from our _form.php view:




<fieldset>

        <legend>Address Details</legend>

		<div>

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

			<?php echo $form->textField($model,'address_street_1',array('size'=>60,'maxlength'=>255,'placeholder'=>'Street 1')); ?>

		</div>

		......


</fieldset>




<div>

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

	<?php echo $form->checkBox($model, 'billing_same', array('value'=>'Yes', 'uncheckValue'=>'No')); ?>

</div>




<fieldset>

        <legend>Billing Details</legend>

		<div>

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

			<?php echo $form->textField($model,'billing_address_street_1',array('size'=>60,'maxlength'=>255,'placeholder'=>'Street 1')); ?>

		</div>

		......


</fieldset>



In the actionIndex do i need a if statement which would loop through something similar to:

form.billing_address_street_1.value = form.address_street_1.value;

??

Any help would be most appreciated

Thank you

Why do you want it to be handled in the controller? You just need a small piece of JavaScript code to do the loop you mentioned and copy values. Just use the registerScript method to keep it in same view. Or better, read my latest article on keeping your JS code clean and separated.

Hi

Thank you for your help, I have read through the links you recommended and in my _form.php view I added the following which indeed renders the script into the head of the page.

Please could you advise on where I have got the syntax wrong or recommend a reference?

Once again, many thanks for your help on this topic.




Yii::app()->clientScript->registerScript('shipsame', '

function shipsame(){

    if(form.billing_same.checked)

      {

        form.billing_address_street_1.value = form.address_street_1.value;

      }

      else

        {

          form.billing_address_street_1.value = "";

        }

} 

', CClientScript::POS_HEAD);

?>


	......




<?php echo $form->checkBox($model, 'billing_same', array('size'=>20,'maxlength'=>20, 'value'=>'Yes', 'uncheckValue'=>'No', 'onclick'=>'shipsame(this.form)')); ?>


	......



You haven’t declared an argument in the shipsame() function. Also, you pass this.form when calling it but I don’t think that an input field has a form property.

Hey GPM,

Did you get a so

lution to the above problem?