[英]Selected value get from DB into dropdown select box
I have an Employee Form Edit.我有一个员工表格编辑。 My problem is, my function for the edit is working fine but I can't get the value of my employee type.
我的问题是,我的编辑功能运行良好,但我无法获取员工类型的值。
I have 4 employee type:我有 4 种员工类型:
Every time I go to edit form, the employee type on drop down is always on staff (I want to display, if the employee is "Manager" then the drop box show Manager).每次我去编辑表单时,下拉列表中的员工类型总是在员工上(我想显示,如果员工是“经理”,那么下拉框显示经理)。
This is the code for editroledetails.blade.php这是editroledetails.blade.php的代码
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
<option value="Staff">Staff</option>
<option value="Supervisor">Supervisor</option>
<option value="Manager">Manager</option>
<option value="Super User">Super User</option>
</select>
</div>
Controller function控制器功能
public function editroledetails(Request $request)
{
$user = \Auth::user();
$userphone = 0;
$reportTo = DB::select(DB::raw("select username from customer_type where customer_type = 'Supervisor' or customer_type ='Manager' "));
$select = DB::select(DB::raw("select customer_type from customer_type "));
$data = [
'editUsername' => $request->editUsername,
'editNik' => $request->editNik,
'editEmail' => $request->editEmail,
'editRegIdentities' => $request->editRegIdentities,
'editReportTo' => $request->editReportTo,
'editID' => $request->editID
];
return view('editroledetails', compact('user', 'userphone', 'data', 'reportTo', 'select'));
}
You can do something like this:你可以这样做:
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
@if($user->customer_type == 'Staff')
<option value="Staff">Staff</option>
@endif
@if($user->customer_type == 'Supervisor')
<option value="Supervisor">Supervisor</option>
@endif
@if($user->customer_type == 'Manager')
<option value="Manager">Manager</option>
@endif
@if($user->customer_type == 'Super User')
<option value="Super User">Super User</option>
@endif
</select>
</div>
Use the selected
attribute on the select options在选择选项上使用
selected
属性
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
<option value="Staff" @if($user->customer_type == 'Staff')selected@endIf>Staff</option>
<option value="Supervisor" @if($user->customer_type == 'Supervisor')selected@endIf>Supervisor</option>
<option value="Manager" @if($user->customer_type == 'Manager')selected@endIf>Manager</option>
<option value="Super User" @if($user->customer_type == 'Super User')selected@endIf>Super User</option>
</select>
</div>
You can use this to show the selected item in select list您可以使用它来显示选择列表中的所选项目
<?php
$customerTypes = [
'Staff' => 'Staff',
'Supervisor' => 'Supervisor',
'Manager' => 'Manager',
'Super User' => 'Super User',
];
?>
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
@foreach($customerTypes as $type)
<?php
$selected = "";
@if($user->customer_type == $type) {
$selected = "selected";
}
?>
<option value="{{ $type }}" {{ $selected }} >{{ $type }}</option>
@endforeach
</select>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.