简体   繁体   中英

How to retrieve selected option's value in Laravel 5 dropdown list?

My dropdown list is prepopulated with names using a 'Users' table. I wish to use the selected name's id to then fill a table underneath the dropdown list with that person's email, also stored in the same 'Users' table. Any ideas on how I should do that while keeping everything safe?

My form:

<div class="form-group">
    <label for="employee-select" class="control-label col-md-3">Select Employee</label>
       <div class="col-md-8">
          <div class="col-md-10">
             {{Form::select('user',$users, null, ['placeholder' => 'Choose employee...', 'class'=> 'select-block-level chzn-select'])}}
           </div>
       </div>
 </div>

The controller:

public function create()
    {
        $users = User::lists('name','id');

        return View::make('sickleaves.create')->with('users',$users);
    }

routes.php:

Route::resource('sickleaves','SickLeaveController');

I assuming that you're using resource controller and you're sending form to a store() action. In this case you'll have access to selected user with:

public function store(Request $request)
{
    echo $request->user;
function updateText() {
        var empName=$('#userdropdown :selected').text();
        empName = empName.split(' ');

        document.getElementById("firstname").value = empName[0];
        document.getElementById("surname").value = empName [empName.length - 1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM