选项值默认
问题描述:
我有我的看法这一点,选项值默认
<div class="form-group">
<label for="theme" class="col-sm-3 control-label">Theme</label>
<select class="form-control" id="theme" name="theme">
<option value="0">Default Theme</option>
<option value="1">Dark Theme</option>
</select>
</div>
,并在我的控制器
/**
* Change User Account Settings
*
* @access public
* @return view user.settings
*/
public function changeSettings($username, $id)
{
$user = Auth::user();
if (Request::isMethod('post')) {
$user->style = (int) Request::get('theme');
$user->save();
return Redirect::route('profil', ['username' => $user->username, 'id' => $user->id])->with(Toastr::success('Your Account Was Updated Successfully!', 'Yay!', ['options']));
} else {
return redirect()->back()->with(Toastr::warning('Something Went Wrong!', 'Error', ['options']));
}
}
我怎样才能使它如此选择值默认为用户选择什么风格的选项在数据库中?
答
在你看来,你做它像这样:
<option value="0" @if($selectedTheme == 0) SELECTED @endif>Default Theme</option>
<option value="1" @if($selectedTheme == 1) SELECTED @endif>Some other theme Theme</option>
(当然,改变$selectedTheme
到任何由你的控制器返回。)
感谢完美地工作 –