在我的编辑表单的一部分中,我有复选框。我可以很好地提交表单,这些值以逗号分隔的值存储在数据库中。苹果、微软等。这很好,但当我回到编辑表单时,复选框没有被选中。我试着回显,检查是否满足以下条件:
<input name="software[]" type="checkbox" value="Ortho trac" <?php if(isset($_POST['software'])) echo "checked"; ?>> Ortho trac
但是上面的代码不起作用。我不知道如何在拉拉韦尔做这件事当我尝试更新值时,数据库中以前的值也会消失
视图
<div class="form-group mtop-20">
<label for="temp_software_experience">Software Experience:</label><br>
<label class="checkbox-inline">
<input name="software[]" type="checkbox" class="input-sm" value="Practiceworks"> Practiceworks
</label>
<label class="checkbox-inline">
<input name="software[]" type="checkbox" value="Softdent"> Softdent
</label>
<label class="checkbox-inline">
<input name="software[]" type="checkbox" value="Ortho trac" <?php if (isset($_POST['software'])) echo "checked"; ?>> Ortho trac
</label>
<label class="checkbox-inline">
<input name="software[]" type="checkbox" value="Dentrix"> Dentrix
</label>
<label class="checkbox-inline">
<input name="software[]" type="checkbox" value="Easy Dental"> Easy Dental
</label>
<label class="checkbox-inline">
<input name="software[]" type="checkbox" value="Practiceworks"> Eaglesoft
</label>
<label class="checkbox-inline">
<input type="checkbox" id="temp_software_experience" value="Other"> Other
</label>
</div><!-- end .form-group -->
控制器
public function update($id)
{
// validate
// read more on validation at http://laravel.com/docs/validation
$rules = array(
'firstname' => 'required|min:3|alpha',
'lastname' => 'required|min:3|alpha',
'zipcode' => 'required|min:5|numeric',
'temp_experience' => 'required|min:1|max:50|numeric',
'temp_travel' => 'required|numeric',
'temp_hourly_rate' => 'required|numeric|min:10',
);
$validator = Validator::make(Input::all(), $rules);
// process the login
if ($validator->fails()) {
return Redirect::to('user/profile/' . $id . '/edit')
->withErrors($validator)
->withInput();
} else {
$software_checked = Input::get('software');
if(is_array($software_checked))
{
$implade_software = implode(',', $software_checked);
}
// store
$user = User::find($id);
$user->firstname = Input::get('firstname');
$user->lastname = Input::get('lastname');
$user->zipcode = Input::get('zipcode');
$user->temp_experience = Input::get('temp_experience');
$user->temp_travel = Input::get('temp_travel');
$user->temp_hourly_rate = Input::get('temp_hourly_rate');
$user->temp_software_experience = $implade_software;
$user->save();
// redirect
Session::flash('message', 'Successfully updated profile!!');
return Redirect::to('user/profile/'.$id.'');
}
}
更新后将重定向,从而清除POST
数据。
// redirect
Session::flash('message', 'Successfully updated profile!!');
return Redirect::to('user/profile/'.$id.'');
要访问这些数据,您需要从存储方法(本例中为数据库)中获取,而不是从POST
数据中获取,因为它不包含任何内容。
所以这个条件应该是这样的:
<?php if(!empty($user->software)) echo "checked"; ?>
注意:即使在视图中也应该使用Input::get()
,使用$_POST
毫无意义
从评论中编辑:
丢失数据的原因是,在对数据执行条件检查之前,首先需要explode()
数据
假设您选择:
[x] Apple
[ ] Orange
[x] Banana
在您的专栏中,它看起来像apple,banana
。
当你执行条件检查时,它看起来类似于:
if ('apple,banana' == 'apple') echo 'checked';
您需要检查逗号分隔列表中的某个元素是否存在。所以试着先爆炸,然后再检查。这应该是一个很好的起点。
$software = explode(',', $user->temp_software_experience);
然后对于每个输入:
if (in_array("Practiceworks", $software)) echo "checked";
这是in_array()的文档。
希望这能澄清一些事情。