I have a drop down list in my Laravel project that store data in my SQL DB however in my edit page, everything is able to be changed but the drop down menu selection so I am not sure what I am doing wrong
here is my code
Controller:
public function update(Request $request, $energykey)
{
$energy->softwareversion = $request->input('softwaretype') == "Old" ? "Old System" : "New System";
}
view:
<div>
<select name="softwaretype">
<option value="">--Select Type--</option>
<option {{ $energy->softwareversion == 'Old System' ? 'selected':'' }}>Old System</option>
<option {{ $energy->softwareversion == 'New System' ? 'selected':'' }}>New System</option>
</select>
</div>
In your controller you have to just get the option value
public function update(Request $request, $energykey)
{
$energy->softwareversion = $request->input('softwaretype');
}
In your view make these changes so you can get the values in controller
<div>
<select name="softwaretype">
<option value="">--Select Type--</option>
<option value="Old System" {{ $energy->softwareversion == 'Old System' ? 'selected':'' }}>Old System</option>
<option value="New System" {{ $energy->softwareversion == 'New System' ? 'selected':'' }}>New System</option>
</select>
</div>
Related
I know this question is asked many time but don't know why solution is not working for me. I am a beginner and have a very simple confusion. I have a form in Laravel blade with a drop down. Now I want to keep the drop down value selected after the user submit the form. This is the code I applied to make it work from internet but don't know why it is not working.
<select class="form-control" name="company">
<option value="select">Select Type</option>
<option value="Apple" {{ old('company') == "Apple" ? 'selected' : '' }}>Apple</option>
<option value="Google" {{ old('company') =="Google" ? 'selected' : '' }}>Google</option>
<option value="Mi" {{ old('company') == "Mi"? 'selected' : '' }}>Mi</option>
<option value="Samsung" {{ old('company') == "Samsung" ? 'selected' : '' }}>Samsung</option>
</select>
I have this form group:
<select class="form-control" id="sel1" name="status">
#if((auth()->user()->two_factor_auth) === 'off')
<option value="off">Off</option>
<option value="sms">SMS</option>
#else
<option value="off">Off</option>
<option value="sms">SMS</option>
#endif
</select>
And as the action, I added this to the method submit of ProfileController:
public function submit(Request $request)
{
if($data['status'] === 'off'){
$request->user()->update([
'two_factor_auth' => 'off'
]);
}else{...}
}
And then at the DB,users table, I have added two_factor_auth column and it is set to sms by default.
So if the user selects, off, it should update the column and set off. But now the problem is it does not do that somehow...
So what is going wrong here ? How can I fix this issue ?
I would really appreciate any idea or suggestion from you guys...
Thanks in advance.
Your html need to be replaced, add the selected attribute to the option you find in your DB/object
<select class="form-control" id="sel1" name="status">
#if((auth()->user()->two_factor_auth) === 'off')
<option selected value="off">Off</option>
<option value="sms">SMS</option>
#else
<option value="off">Off</option>
<option selected value="sms">SMS</option>
#endif
</select>
You can also do in in just two lines
#php
$isOff = auth()->user()->two_factor_auth === 'off';
#endphp
<select class="form-control" id="sel1" name="status">
<option #if($isOff) selected #endif value="off">Off</option>
<option #if(!$isOff) selected #endif value="sms">SMS</option>
</select>
As for the controller, maybe you did not post all the code, but try this
public function submit(Request $request)
{
//you should add validation rules here
$user = auth()->user();
$user->status = $request->input('status', 'sms');//second attribute is for default
//you can add other attributes to update here
$user->save();
}
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:
Staff
Supervisor
Manager
Super User
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
<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
<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>
In my create.blade.php I have a dropdown list which is like that.
<div class="form-group">
<label for="company-content">Sexe</label>
<select name="sex" id="" class="form-control">
<option value="">Choice</option>
<option>Women</option>
<option>Man</option>
</select>
</div>
If I choose the option 2, that is to say the item man, in my edit.blade.php I will wish to get the item man.
However, when I want to change the item, my dropdown list is always on woman bij default. It's not practical...
Here is my code concerning the file edit.blade.php, do you have an idea please?
Thank you
<div class="form-group">
<label for="company-content">Sex</label>
<select name="sexe" id="" class="form-control">
<option>Women</option>
<option>Man</option>
</select>
</div>
Edit: 16/03/2019
Visibly, I should do several modifications on my Controller too?
My function edit
public function edit($id)
{
//
$candidats = Candidat::find($id);
$permis = Permis::all();
return view('admin.candidats.edit', compact('candidats', 'permis'));
}
My function update
public function update(Request $request, $id)
{
$request->validate([
'sexe' => 'required|string',
'fk_permis' => 'required'
]);
$candidats = Candidat::find($id);
$candidats->sexe = $request->get('sexe');
$candidats->fk_permis = $request->get('fk_permis');
$candidats->save();
return redirect()->route('candidats.index')
->with('success', 'mise à jour effectuée');
}
Where I should add this line please?
return view('admin.candidats.edit', ['data' => $data]);
Here is my edit.blade.php
<div class="form-group">
<label for="company-content">Sex</label>
<select name="sexe" class="form-control" >
<option value="Man" {{ $data['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $data['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
</div>
So, my problem is in my Controller?
for laravel from controller return view with data like
return view('edit', ['data' => $data]);
<select name="sexe" class="form-control" >
<option value="Man" {{ $data['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $data['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
and for php
<select name="sexe" class="form-control" >
<option value="Man" {{ $_GET['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $_GET['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
Assuming you're POSTing your data in a form, and have included values as suggested by #Second2None, you need to tell your form to select the relevant option.
<option<?php if ($_POST['sexe'] == 'man') echo " selected"; ?>>Man</option>
I had this problem while submitting form
blade
{{ Form::label('qualification', '') }}</th><td>{{ Form::select('qualification', $qualification, $jobs_list->qualification,array('class' => 'form-control')) }}
In this $qualification my full database list is there and $jobs_list->qualification is my selected value.but again am changing value and submitting form it stored as number because ex : value ="numbers"
controller:
$field = ToQualification::select('value')->get();
$qualification = array();
foreach($field as $user_qualification){
$qualification[] = $user_qualification->value;
}
generated html by blade code:
<select class="form-control" id="qualification" name="qualification">
<option value="0">Associate's</option>
<option value="1">Bachelor's</option>
<option value="2">Diploma</option>
<option value="3" selected="selected">Doctoral</option>
<option value="4">High Schools</option>
<option value="5">Master's</option>
</select>
anyone tell how to display name and id in table same as value in selection box?