doushang1890 2014-01-25 14:55
浏览 16

Yii表格输入

I want to add multiple records from a form into a table.

I have tried to understand and follow this guide and failed

Yii tabular input

http://www.yiiframework.com/doc/guide/1.1/en/form.table

My code does not save.

I have a comment in my controller code written A, here with the isset() function added

this part of the code will not run.

if(isset($_POST['MultiPart2'][$i]))

But when I removed the isset() function I get the error Undefined offset: 0

This means this code

$_POST['MultiPart2'][$i]

Is not working.

What should I do?

Here is the var_dump of my post

$_POST['MultiPart2']

array(2) {
   ["[0"]=>
   array(3) {
     ["'name'"]=>
     string(3) "Vic"
     ["'age'"]=>
     string(2) "25"
     ["'sex'"]=>
     string(1) "m"
   }
  ["[1"]=>
  array(3) {
     ["'name'"]=>
     string(3) "Vic"
     ["'age'"]=>
     string(2) "25"
     ["'sex'"]=>
     string(1) "m"
   }
}

Here is my controller code

 public function actionCreate()
 {

    //insert multiple instances of model into array
    $model=array();
    for ($i=0; $i < 2; $i++) { 
        $model[]=new MultiPart2;
    }

    if(isset($_POST['MultiPart2']))
    {   
        foreach ($model as $i => $model) {

            if(isset($_POST['MultiPart2'][$i]))
             // A
            //with isset() the following code does not run
            {       

                $model->attributes=$_POST['MultiPart2'][$i];
                if($model->save())
                    echo "Saved";
            }
        }
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

And here is my view code

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'multi-part2-form',
'enableAjaxValidation'=>false,
)); ?>

<?php echo $form->errorSummary($model); ?>
    <?php 
    foreach ($model as $i => $model) {
            ?>

                <div class="row">
                    <?php echo $form->labelEx($model,'name'); ?>
                    <?php echo $form->textField($model,"[$i][name]",array('size'=>60,'maxlength'=>100, "value"=>"Vic")); ?>
                    <?php echo $form->error($model,'name'); ?>
                </div>

                <div class="row">
                    <?php echo $form->labelEx($model,'age'); ?>
                    <?php echo $form->textField($model,"[$i][age]",array('size'=>60,'maxlength'=>100, "value"=>"5")); ?>
                    <?php echo $form->error($model,'age'); ?>
                </div>

                <div class="row">
                    <?php echo $form->labelEx($model,'sex'); ?>
                    <?php echo $form->textField($model,"[$i][sex]",array('size'=>60,'maxlength'=>100, "value"=>"m")); ?>
                    <?php echo $form->error($model,'sex'); ?>
                </div> 
                <hr>
            <?php
        }

     ?>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

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

Here is the generated HTML

<form id="multi-part2-form" action="/sam.com/system/index.php?r=multiPart2/create"      method="post">
    <p class="note">Fields with <span class="required">*</span> are required.</p>



<div class="row">
 <label for="MultiPart2_name"   class="required">Name <span class="required">*</span>   </label>                           
 <input size="60" maxlength="100"   value="Vic" name="MultiPart2[[0][name]]" id="MultiPart2__0_name" type="text" />                     

</div>

<div class="row">
<label for="MultiPart2_age" class="required">Age <span class="required">*</span></label>                        
<input size="60" maxlength="100" value="5" name="MultiPart2[[0][age]]" id="MultiPart2__0_age" type="text" />                                            

</div>

<div class="row">
<label for="MultiPart2_sex" class="required">Sex <span class="required">*</span></label>                        
<input size="60" maxlength="100" value="m" name="MultiPart2[[0][sex]]" id="MultiPart2__0_sex" type="text" />                                              
</div> 

<hr>

<div class="row">
<label for="MultiPart2_name" class="required">Name <span class="required">*</span></label>                      
<input size="60" maxlength="100" value="Vic" name="MultiPart2[[1][name]]" id="MultiPart2__1_name" type="text" />                        

</div>

<div class="row">

<label for="MultiPart2_age" class="required">Age <span class="required">*</span></label>                          
<input size="60" maxlength="100" value="5" name="MultiPart2[[1][age]]" id="MultiPart2__1_age" type="text" />                                            
</div>

<div class="row">
<label for="MultiPart2_sex" class="required">Sex <span class="required">*</span></label>                        
<input size="60" maxlength="100" value="m" name="MultiPart2[[1][sex]]" id="MultiPart2__1_sex" type="text" />                                             
</div> 

<hr>

<div class="row buttons">
<input type="submit" name="yt0" value="Create" />   </div>

</form>
  • 写回答

2条回答 默认 最新

  • duanbaque4230 2014-01-25 14:59
    关注

    You should change your view code as below:

    Remove '' around name in "[$i]['name']" - and in all fields also

    <?php echo $form->textField($model,"[$i][name]",array('size'=>60,'maxlength'=>100, "value"=>"Vic")); ?>
    

    And also change the variable name in foreach as Voodoo417 said.

    foreach ($model as $i => $mod) {
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)