user11124425
user11124425

Reputation: 971

Set Default option in dropdown list HTML

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. enter image description here

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

enter image description here

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

Upvotes: 0

Views: 273

Answers (2)

Palak Jadav
Palak Jadav

Reputation: 1264

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>

Upvotes: 1

maganthro
maganthro

Reputation: 459

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>

Upvotes: 1

Related Questions