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>
Related
invoices/index.blade.php
<td>
<form action="{{url('/invoice_status_upadated')}}" method="POST">
{{ csrf_field() }}
<div class="input-group mb-3">
<select class="form-select" aria-label="Default select example">
<option value="0" {{$inv->status == 0 ? 'selected':''}}>Pending </option>
<option value="1" {{$inv->status == 1 ? 'selected':''}}>In Process </option>
<option value="2" {{$inv->status == 2 ? 'selected':''}}>Completed </option>
<option value="3" {{$inv->status == 3 ? 'selected':''}}>Cancelled </option>
<?php
if ($inv->status == 0){
echo "selected";
}
?>
<?php
if ($inv->status == 1){
echo "selected";
}
?>
<?php
if ($inv->status == 2){
echo "selected";
}
?>
<?php
if ($inv->status == 3){
echo "selected";
}
?>
</select>
<button type="submit" class="btn btn-outline-success">Update</button>
</div>
</form>
</td>
/TemplateContorller
public function invoice_status_upadated(Request $request){
$data= Invoice::find($request->status);
$data->status=$request->$data->status;
$data->save();
return redirect('invoices');
}
web.php
Route::post('/invoice_status_upadated', 'App\Http\Controllers\TemplateController#invoice_status_upadated')->name('invoice_status_upadated');
View
error
Try this :
<select class="form-select" aria-label="Default select example" name="status">
And also in your controller you have to change :-
$data = Invoice::find($invoice_id);
$data->status = $request->status;
$data->save();
Hope this will work for you.
Instead of doing $data->status=$request->$data->status; do $data->status = $request->status;
$request does not have the data from the model, which is in the $data variable.
Hi first of all you need to async you select tag with name tag and set it to status
for example
<select class="form-select" aria-label="Default select example" name="status">
<input type="hidden" value="{{ $inv->id }}" name="inv_id"/>
then in your controller first of all you need to validate your request incomping data for mor information please see this in laravel documentations
after that you need to change your code like this
$data= Invoice::find($request->inv_id);
$data->status= $data->status;
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 want to get the Value of it to insert it into my Games table in database. But I have to show the name of genre from the Genres table in database.
View:
<select name="genreid">
#foreach($genres as $genre)
<option value="{{ $genre->genreid }}"> {{ $genre->genre }}</option>
#endforeach
</select>
Controller:
public function insertgame(Request $request){
$this -> validate($request, array(
'gamename' => 'required|min:3',
'price' => 'required|int|min:1',
'genre' => 'required',
'releaseddate' => 'required|date',
'picture' => 'required|mimes:jpeg,jpg,png,gif'
));
$gamename = $request->input('gamename');
$genreid = $request->input('genreid');
$price = $request->input('price');
$releaseddate = Carbon::parse($request->input('releaseddate'));
$picture = $request->file('picture')->getClientOriginalName();
$data=array('gamename' => $gamename, 'genreid'=>$genreid, 'price'=>$price,'releaseddate'=>$releaseddate,'picture'=>$picture );
DB::table('games')->insert($data);
return redirect('managegame');
}
You've said it works for you if you use this HTML:
<select name="genre">
<option selected disabled>Choose one</option>
<option value="FPS">FPS</option>
<option value="Action">Sports</option>
<option value="Action">Action</option>
<option value="Racing">Racing</option>
<option value="Simulation">Simulation</option>
</select>
In this case, to use proper data format, change this:
<option value="{{ $genre->genreid }}">{{ $genre->genre }}</option>
To:
<option value="{{ $genre->genre }}">{{ $genre->genre }}</option>
Do something like this
#foreach($game->genres as $genre)
{
<option value='{{$genre->id}}' {{$game->genre ==$genre->id ? 'selected' :' '> {{$genre->name}} </option>
}
I've found the answer!
I just changed
'genre' => 'required',
to
'genreid' => 'integer|required',
LOL Thanks all
I have a problem regarding insertion of data after explode. In this gridview column, I throw function in value like this:
[
'attribute' => 'CONNECTOR_ACTION',
'value' => function($model){
$apps = \app\models\APPLICATION::find()
->where(['ID' => $model->ID_APPLICATION])
->one();
$options = $apps['CONNECTOR_PARAM'];
$optionsArr = explode(', ', $options);
return Html::activeDropDownList($model, 'CONNECTOR_ACTION', $optionsArr, ['class'=>'form-control', 'disabled' => true]);
},
'format' => 'raw'
],
And in HTML view like this:
<td>
<select id="requestapplication-connector_action" class="form-control" name="REQUESTAPPLICATION[CONNECTOR_ACTION]" disabled>
<option value="0">create</option>
<option value="1">addrole</option>
<option value="2">defaultrole</option>
<option value="3">removerole</option>
<option value="4" selected>disable</option>
<option value="5">enable</option>
<option value="6">setpassword</option>
</select>
</td>
If I want to change dropdown like this:
<td>
<select id="requestapplication-connector_action" class="form-control" name="REQUESTAPPLICATION[CONNECTOR_ACTION]" disabled>
<option value="create">create</option>
<option value="addrole">addrole</option>
<option value="defaultrole">defaultrole</option>
<option value="removerole">removerole</option>
<option value="disable" selected>disable</option>
<option value="enable">enable</option>
<option value="6">setpassword</option>
</select>
</td>
How do I do that?
This is because the keys of $optionsArr are 0-6 instead of the values. To combine it set the keys the same as the values.
$combined = array_combine($optionsArr, $optionsArr);
And then use $combined in Html::activeDropdownList()
I am new in OOP PHP and I am still learning, I have a form where name of locations is populated through JQuery/HTML form using drop-down field with same name, my problem is when I try to insert in mysql database, I did not get the selected value,
<div id="NCR" class="drop-down-show-hide">
<div class="field">
<label for="region"> NCR </label>
<select name="region" id="ncr region">
<option value="">-- Choose --</option>
<option value="1"> Region 1 </option>
<option value="2"> Region 2 </option>
<option value="3"> Region 3 </option>
</select>
</div>
</div>
<div id="CAR" class="drop-down-show-hide">
<div class="field">
<label for="field"> CAR </label>
<select name="region" id="car region">
<option value="">-- Choose --</option>
<option value="4"> Region 4 </option>
<option value="5"> Region 5 </option>
<option value="6"> Region 6 </option>
</select>
</div>
</div>
<div id="region3" class="drop-down-show-hide">
<div class="field">
<label for="region"> CAMANABA </label>
<select name="region" id="camanaba region">
<option value="">-- Choose --</option>
<option value="7"> Region 7 </option>
<option value="8"> Region 8 </option>
<option value="9"> Region 9 </option>
</select>
</div>
</div>/HTML
PHP
if(Token::check(Input::get('token'))){
$validate = new Validate();
$validation = $validate->check($_POST, array(
'apz_account' => array(
'required' => true
),
'api_account' => array(
'required' => true
),
'wupos_tid' => array(
'required' => true
),
));
if($validation->passed()){
// check APZ account
$agent = DB::getInstance()->get('agent', array('apz_account', '=', Input::get('apz_account')));
if(!$agent->count()){
// check API account
$agent = DB::getInstance()->get('agent', array('api_account', '=', Input::get('api_account')));
if(!$agent->count()){
$agent = new Agent();
try {
$agent->createAgentAccount(array(
'apz_account' => Input::get('apz_account'),
'api_account' => Input::get('api_account'),
'wupos_tid' => Input::get('wupos_tid'),
'region' => Input::get('region'),
'registered' => date('Y-m-d H:i:s'),
));
// print_r($agent);
// Session::flash('success', 'You have registered successfully.');
// Redirect::to('../../index.php');
} catch(Exception $e){
die($e->getMessage());
}
} else {
echo Input::get('api_account'), ' account is already exists';
}
} else {
echo Input::get('apz_account'), ' account is already exists';
}
} else {
foreach ($validation->errors() as $error) {
echo $error, '<br>';
}
}
}
Please advice. Thank you
You need unique names for each form element, with php you can use region[]. this will give you an array in your server script.
Then when you get the value do current(array_filter(Input::get('region')));