How to Add textfield in form when link is clicked

Hey guys, i want ask something. I want to add an extra textField when i click button to my form. Please help me

A simple variant:

In your form view:




<?php Yii::app()->clientScript->registerScript('toggler','$("#optional-link").bind("click",function(){$("#optional-text").toggle();})')?>


<?php echo CHtml::link('Show/hide additional input','#',array('id'=>'optional-link')); ?>

<div id="optional-text">

    <?php echo CHtml::textfield('optional_text','');?>

</div>



thanks but i want to add textfield, for example i have 1 textfield when i click button then i have 2 textfield. Can you help me please and i want it process like array

Ok, but please be more specific next time :)

If pure client side code fits your needs then here is an example (although very simple):




<?php Yii::app()->clientScript->registerScript('textFieldAdder','$("#additional-link").bind("click",function(){

    var id="optional_text";

    var size=$("#additional-inputs > li input").size();

    $("#additional-inputs").append("<li><input type=text id="+id+size+" name="+id+"["+size+"]></li>");

    })')?>


<?php $form = $this->beginWidget('CActiveForm', array(

    'id'=>'user-form',

    'enableAjaxValidation'=>true,

)); ?>

<?php echo CHtml::link('Add input','#',array('id'=>'additional-link')); ?>

<ul>

<div id="additional-inputs">

    <li><?php echo CHtml::textfield('optional_text[0]','');?></li>

</div>

</ul>

<?php echo CHtml::submitButton('Submit');?>

<?php $this->endWidget('CActiveForm');?>



When submitting this form you will receive an array called optional_text with integer keys holding the values [0]=>firstvalue,[1]=>secondvalue. If you need a more complex solution using additonal models and stuff take a look at this wiki entry by zaccharia http://www.yiiframework.com/wiki/141/javascript-tabular-input

Thanks for your respons

hi,

:) thanks.its very helpful for me.

how i can remove extra added one?

Thanks Haensel .

it was really useful :)

thanks for sharing

thank u Haensel :P)

Sorry, I am a newbie

I want to ask you

How to save data from add new field to database?