Yii Framework Forum: How to Add textfield in form when link is clicked - Yii Framework Forum

Jump to content

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

How to Add textfield in form when link is clicked Rate Topic: -----

#1 User is offline   prasetutama 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 29-June 11

Posted 16 September 2011 - 06:58 AM

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

#2 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 16 September 2011 - 07:31 AM

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>

1

#3 User is offline   prasetutama 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 29-June 11

Posted 16 September 2011 - 07:39 AM

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
0

#4 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 16 September 2011 - 08:36 AM

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.yiiframew...t-tabular-input
1

#5 User is offline   prasetutama 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 29-June 11

Posted 16 September 2011 - 10:27 AM

Thanks for your respons
0

#6 User is offline   Shobhith C 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 10-January 13
  • Location:Cochin

Posted 12 January 2013 - 07:33 AM

hi,
:) thanks.its very helpful for me.
how i can remove extra added one?
0

#7 User is offline   vijay24 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 18-January 14

Posted 21 January 2014 - 02:34 AM

Thanks Haensel .
it was really useful :)
0

#8 User is offline   SkingTop 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 20-June 14

Posted 28 June 2014 - 04:01 AM

thanks for sharing
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