nsr nsr - 1 month ago 13
PHP Question

Foreach loop for active form

i want to use foreach loop for activeform in yii2, im using normal active form for this, its coming fine, but while submit both the fom data is bieng same.. Here is my code..

<?php foreach ($addeddocs as $docs ) { ?>
<div class="col-md-push-2 col-md-8" >
<?php $form = ActiveForm::begin(['id' => 'requestacallform','action' => 'assign-upload','method' => 'post']);
?>
<p><b><?php echo $docs->document_name; ?> </b></p>
<a target="_blank" href="<?= Url::to([$baseurl])?>/user/purchase_order/<?php echo $docs->uploaded_file; ?>" ><?php echo substr("$docs->uploaded_file",10); ?></a>
<div class="form-group">
<div class="col-xs-4 col-md-4 ">
<?= $form->field($docs, 'uploaded_file')->hiddenInput(['value'=>$docs->uploaded_file])->label(false); ?>
<?= $form->field($docs, 'project_id')->dropDownList($projects,
['multiple' => 'multiple'])->label(false); ?>
</div>
</div>
<?php echo Html::submitButton('Submit', array('id' => 'draft', 'class' => 'btn btn-default hvr-shutter-out-horizontal',
));?><br>
<?php ActiveForm::end(); ?>
</div>
<?php }?>

Answer

Remove the 'id' => 'requestacallform' form your form, or give the id as I varible and increase it after the loop, so that it gets new id for every form.

This should be the code.

<?php foreach ($addeddocs as $docs ) {   ?>
<div class="col-md-push-2 col-md-8" >
 <?php $form = ActiveForm::begin(['action' => 'assign-upload','method' => 'post']);
        ?>
       <p><b><?php echo $docs->document_name; ?> </b></p>
       <a target="_blank" href="<?= Url::to([$baseurl])?>/user/purchase_order/<?php echo $docs->uploaded_file; ?>" ><?php echo substr("$docs->uploaded_file",10); ?></a>
       <div class="form-group">
          <div class="col-xs-4  col-md-4 "> 
         <?= $form->field($docs, 'uploaded_file')->hiddenInput(['value'=>$docs->uploaded_file])->label(false); ?> 
         <?=  $form->field($docs, 'project_id')->dropDownList($projects,
                       ['multiple' => 'multiple'])->label(false); ?>  
          </div>  
      </div> 
  <?php echo Html::submitButton('Submit', array('id' => 'draft', 'class' => 'btn btn-default hvr-shutter-out-horizontal',
            ));?><br>
        <?php ActiveForm::end(); ?>
 </div>