Set Default option in dropdown list HTML - php

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>

Related

JSON column in Laravel does not receive values of others columns

I'm trying to create a json that collects all the information from the other columns, but the following error happens when I create a new record:
SQLSTATE[HY000]: General error: 1364 Field 'properties' doesn't have a default value
Migration
public function up()
{
Schema::create('hunters', function (Blueprint $table) {
$table->id();
$table->string('name_hunter', 50);
$table->integer('year_hunter');
$table->decimal('height_hunter', 3,2);
$table->decimal('weight_hunter', 5,2);
$table->string('type_hunter', 30);
$table->string('type_nen', 30);
$table->string('type_sangue', 3);
$table->timestamp('register_date')->useCurrent();
$table->timestamp('data_updated')->useCurrent()->useCurrentOnUpdate();
$table->json('properties');
});
}
HunterModel.php
protected $casts = [
'properties' => 'array'
];
HunterController.php
public function create()
{
return view('create');
}
public function store(HunterRequest $request)
{
$validations = $request->validated();
HunterModel::create($validations);
return redirect('/');
}
create.php
<form action="{{ url("create") }}" method="POST">
{{ csrf_field() }}
<div class="form_group">
<div for="nome_hunter">Name:
<input type="text" class="form-control" name="name_hunter" maxlength="50" value="{{ old('name_hunter') }}">
</div>
</div>
<br>
...
<div class="form_group">
<div for="type_blood">Type blood:
<select class="form-control" name="type_blood">
<option {{ old('type_blood') == '' ? 'selected' : ''}} value="">Choose the type blood</option>
<option {{ old('type_blood') == 'A+' ? 'selected' : ''}} value="A+">A+</option>
<option {{ old('type_blood') == 'A-' ? 'selected' : ''}} value="A-">A-</option>
<option {{ old('type_blood') == 'B+' ? 'selected' : ''}} value="B+">B+</option>
<option {{ old('type_blood') == 'B-' ? 'selected' : ''}} value="B-">B-</option>
<option {{ old('type_blood') == 'AB+' ? 'selected' : ''}} value="AB+">AB+</option>
<option {{ old('type_blood') == 'AB-' ? 'selected' : ''}} value="AB-">AB-</option>
<option {{ old('type_blood') == 'O+' ? 'selected' : ''}} value="O+">O+</option>
<option {{ old('type_blood') == 'O-' ? 'selected' : ''}} value="O-">O-</option>
</select>
</div>
</div>
<br>
</form>
What is the necessary change in the form so that it is possible for the JSON to collect the information?
You need a default value for the properties field. Add this to your migration and re-run it (be sure to revert the last migration).
$table->json('properties')->default(json_encode([]));

I am beginner trying to update status on view table laravel but i am getting controller error that (Attempt to read property "status" on null)

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;

Drop down selection does not update via edit page in Laravel 8

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>

Getting selected option on edit form in laravel 6

i want to edit user profile which has a country, country field is a select option, i want to get a user's country value in select field, i tried the answer in stack but it didn't work for me knowing there is relationship between user and country belongsTo hasMany.
edit.blade.php
<select id="country" name="country_id" class="form-control" >
<option value="" selected disabled>Select</option>
#foreach($countries as $country)
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
#endforeach
</select>
usersController.php
public function edit($id)
{
$users = Auth::user();
$countries = Country::all();
return view('users.edit')->with([
'users' => $users,
'countries' => $countries
]);
}
Instead of this:
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
I believe you want this:
<option value="{{$country->id}}"{{ $country->id == $users->country_id ? ' selected' : '' }}> {{ $country->name }}</option>

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:
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>

Categories