Add a dynamic form

Hello,

I create this form:



<?php echo CHtml::beginForm(); ?>


<?php echo CHtml::errorSummary($formRegister); ?>


<table>


<tr>


<td><?php echo CHtml::activeLabel($formRegister,'time_category'); ?></td>


<td><?php echo CHtml::activeDropDownList($formRegister,'time_category', $timeCategory);?></td>


</tr>


<td><?php echo CHtml::activeLabel($formRegister,'time_donation_desc'); ?></td>


<td><?php echo CHtml::activeTextArea($formRegister,'time_donation_desc',array('cols'=>'35','rows'=>'8'));?></td>


</tr>


</tr>


<td><?php echo CHtml::activeLabel($formRegister,'num_hours'); ?></td>


<td><?php echo CHtml::activeTextField($formRegister,'num_hours',array('size'=>'5'));?></td>


</tr>


<tr>


<td><?php echo CHtml::activeLabel($formRegister,'frequency'); ?></td>


<td><?php echo CHtml::activeDropDownList($formRegister,'frequency', $timeFrequency);?></td>


</tr>


<tr>


<td><?php echo CHtml::activeLabel($formRegister,'renew_check'); ?></td>


<td><?php echo CHtml::activeCheckBox($formRegister,'renew_check');?></td>


</tr>


<tr>


<td><?php echo CHtml::button('Add another time donation'); ?></td>


<td></td>


</tr>


<tr>


<td></td>


<td><?php echo CHtml::submitButton('Send'); ?></td>


</tr>


</table>


<?php echo CHtml::endForm(); ?>


I would like create an identical form when the user click on the 'Add another time donation'. Can you help me to do this? and how I can manipulate the posted data in the controller?

Thanks.

Check this: http://www.yiiframew…uide/form.table

May be this is what you need

Ok for the action into the controller, but I see that the form in this example was created by a foreach statement, I need to create another identic form when the user click in the appropriated button.

May be this?

http://www.yiiframew…oc/cookbook/29/

it seems that this example will help me to resolve my problem, thank you in any case …

You need this files (as example)

index.php

form1.php

form2.php

in your view: (i'm use INDEX as example)

<?php echo CHtml::beginForm(); ?>

<?php include($form.'.php'); ?>

<?php echo CHtml::endForm(); ?>

in your FIRST form (form1.php) create this buttom



<?php echo CHtml::submitButton('Add another time donation', array('submit'=>'', 'params'=>array('otherForm'=>'form2'))); ?>


your form2 (form2.php)



<div class="yiiForm">


     Create your form2 here


 </div>


in your Controller action INDEX (as example)



$form = 'form1'; // already start with first form (file form1.php)





if(isset($_POST['otherForm'])) // change the $form on submit with value params 'otherForm' in submitButton


$form = $_POST['otherForm'];





$this->render('index',array(


'form'=>$form,


));


I hope it help you!  :)

Hello everybody,

I resolved the problem by drawing on the example of adding pictures:

The view:



<div class="yiiForm">


<h2>Donate your time :</h2>


<?php $this->layout= "register"; ?>





<?php echo CHtml::beginForm(); ?>


<?php echo CHtml::errorSummary($formRegister); ?>





<?php foreach($formRegister as $i => $time): ?>





<div id="time-<?php echo $i ?>">


<div class="simple">


<?php echo CHtml::activeLabel($time,'time_category'); ?>


<?php echo CHtml::activeDropDownList($time,"time_category[$i]", $timeCategory);?>


</div>


<div class="simple">


<?php echo CHtml::activeLabel($time,'time_donation_desc'); ?>


<?php echo CHtml::activeTextArea($time,"time_donation_desc[$i]",array('cols'=>'35','rows'=>'8'));?>


</div>


<div class="simple">


<?php echo CHtml::activeLabel($time,'time_num_hours'); ?>


<?php echo CHtml::activeTextField($time,"time_num_hours[$i]",array('size'=>'5'));?>


</div>


<div class="simple">


<?php echo CHtml::activeLabel($time,'time_frequency'); ?>


<?php echo CHtml::activeDropDownList($time,"time_frequency[$i]", $timeFrequency);?>


</div>


<div class="simple">


<?php echo CHtml::activeLabel($time,'time_renew_check'); ?>


<?php echo CHtml::activeCheckBox($time,"time_renew_check[$i]"); ?>


</div>


</div>





<?php endforeach; ?>





<table>


<tr>


<td><?php echo CHtml::button('Add another time donation',array('name'=>'addTime', 'id'=>'addTime')); ?></td>


<td></td>


</tr>


<tr>


<td align="right"><?php echo CHtml::submitButton('Send',array('name'=>'submitDatas')); ?></td>


</tr>


</table>





<?php echo CHtml::endForm(); ?>


</div>





<script type="text/javascript">


 


   /*<![CDATA[*/


   


   var timeAdded = 0;


 


  


   $('#addTime').click(function () {


      


      var divCloned = $('#time-0').clone();


        


      


      $('#time-'+(timeAdded++)).after(divCloned);


   


     


      divCloned.attr('id', 'time-'+timeAdded);


      


     


      initNewInputs(divCloned.children('.simple'), timeAdded);


      initNewSelects(divCloned.children('.simple'), timeAdded);


      initNewAreas(divCloned.children('.simple'), timeAdded);


   });


 


   function initNewInputs( divs, idNumber ) {


      


      var labels = divs.children('label').get();


      for ( var i in labels )


         labels[i].setAttribute('class', 'required');      


 


      var inputs = divs.children('input').get();      


 


      for ( var i in inputs  ) {


         inputs[i].value = "";


         inputs[i].setAttribute('class', '');


         inputs[i].id = inputs[i].id.replace(/d+/, idNumber);


         inputs[i].name = inputs[i].name.replace(/d+/, idNumber);


        


      }


   }


   function initNewSelects( divs, idNumber ) {


	     


	      var labels = divs.children('label').get();


	      for ( var i in labels )


	         labels[i].setAttribute('class', 'required');      


	 


	      var inputs = divs.children('select').get();      


	 


	      for ( var i in inputs  ) {


	         inputs[i].value = "";


	         inputs[i].setAttribute('class', '');


	         inputs[i].id = inputs[i].id.replace(/d+/, idNumber);


	         inputs[i].name = inputs[i].name.replace(/d+/, idNumber);


	      }


	   }


   function initNewAreas( divs, idNumber ) {


	     


	      var labels = divs.children('label').get();


	      for ( var i in labels )


	         labels[i].setAttribute('class', 'required');      


	 


	      var inputs = divs.children('textarea').get();      


	 


	      for ( var i in inputs  ) {


	         inputs[i].value = "";


	         inputs[i].setAttribute('class', '');


	         inputs[i].id = inputs[i].id.replace(/d+/, idNumber);


	         inputs[i].name = inputs[i].name.replace(/d+/, idNumber);


	      }


	   }


 


   /*]]>*/


 


</script>


The controller:



public function actionRegisterStepFourDonor ()


    {


        Yii::app()->getClientScript()->registerCoreScript('jquery');


        $session = new CHttpSession();


        $session->open();


        $email = $session['email'];


        $session->close();


        $formRegister = array(new subscription());


        $recordSaved = false;


        


        if (isset($_POST['subscription'])) {


            $donor = $this->loadUser($email);


            


            foreach ($_POST['subscription'] as $i => $item) {


                $timeDonation[$i] = new time_donation();


                $formRegister[$i] = new subscription();


                if (isset($_POST['subscription'][$i])) {


                    $formRegister[$i]->attributes = $_POST['subscription'][$i];


                    $timeDonation[$i]->id_time_category = $item['time_category'];


                    $timeDonation[$i]->nb_hours = $item['time_num_hours'];


                    $timeDonation[$i]->id_user = $donor->id;


                    $timeDonation[$i]->created_at = date("Y-m-d H-i-s");


                    $timeDonation[$i]->description = $item['time_donation_desc'];


                    $timeDonation[$i]->id_time_frequency = $item['time_frequency'];


                    $timeDonation[$i]->renew_automatically = $item['time_renew_check'];


                    if ($timeDonation[$i]->save()) {


                        $recordSaved =  true;


                    }


                }


            }


            if ($recordSaved) {


                $this->redirect(array('registerSendMessage'));


            }


        }


        $timeCategory = $this->getTimeCategory();


        $timeFrequency = $this->getTimeFrequency();


        $this->render('registerforthstepdonor', array('formRegister' => $formRegister , 'timeCategory' => $timeCategory , 'timeFrequency' => $timeFrequency));


    }


I see that the code is not too optimal, but it works fine … ;)