我想從表格中添加多個記錄。Yii表格輸入
我試圖理解並遵守本指南和失敗的
http://www.yiiframework.com/doc/guide/1.1/en/form.table
我的代碼不會保存。
我在我的控制器代碼中的註釋書面一個,這裏與isset()
功能添加
的代碼將無法運行的這一部分。
if(isset($_POST['MultiPart2'][$i]))
但是當我刪除了isset()函數的功能我得到的錯誤未定義抵消:0
這意味着該代碼
$_POST['MultiPart2'][$i]
不工作。
我該怎麼辦?
這是帖子的的var_dump
$_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"
}
}
這裏是我的控制器代碼
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,
));
}
這裏是我的看法代碼
<?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(); ?>
這裏是生成的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>
我有這樣做,但沒有任何改變。如果添加isset()函數,我仍然會得到最危險的錯誤,或者保存的代碼會跳轉。任何更多的想法... –
只是做print_r($ _ POST);你可以看到數據格式 –
@kumar_v好版 – voodoo417