将选择字段添加到Bootstrap表单并将其发布到Laravel项目中的数据库 [英] Adding Select Field to Bootstrap Form and Getting it to Post to Database in Laravel Project

查看:50
本文介绍了将选择字段添加到Bootstrap表单并将其发布到Laravel项目中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel,并尝试向标准的Bootstrap注册表单中添加一个新的选择字段.提交表单后,我无法获得在选择字段中指定的值以显示在数据库中.其他所有字段(例如,用户名和电子邮件)都可以正常工作.

I am using Laravel and trying to add a new select field to the standard Bootstrap registration form. I cannot get the value specified in the select field to show up in my database after submitting the form. All of the other fields (e.g., user name and email) are working fine.

下面是代码的一部分,为简洁起见删除了一些字段.我要做的就是添加选择字段,而不更改其他Laravel文件中的任何代码.该数据库有两个表.一种称为机构,它具有两列:名称"和"instid"(主键).另一个称为用户,它具有几列,其中一列是"instid"(外键).这个外键字段是我要向其发布所选值的字段.

Below is a portion of the code with some fields removed for brevity. All I've done is add the select field without altering any code in other Laravel files. The DB has two tables. One is called institutions, which has two columns: "name" and "instid" (primary key). The other is called users, which has several columns, one of which is "instid" (foreign key). This foreign key field is the one to which I would like to post the selected value.

提交表单后,如何获取在选择字段中指定的值以显示在数据库中?

How do I get the value specified in the select field to show up in my database after submitting the form?

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Register</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
                        {!! csrf_field() !!}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <!-- use for="name" so that clicking on label places cursor in input field with id="name" -->
                            <label class="col-md-4 control-label" for="name">Name</label>

                            <div class="col-md-6">
                                <input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}">

                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">

                        <div class="form-group">
                            <label class="col-md-4 control-label" for="instid">Institution</label>

                            <div class="col-md-6">
                                <select class="form-control" id="instid" name="instid" required="required">
                                    <option value="" selected disabled>Choose an institution.</option>
                                    <?php
                                        $institutions = App\Institution::all();
                                        foreach($institutions as $institution){
                                            echo "<option value=\"" . $institution->instid . "\">" . $institution->name . "</option>";
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
  
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-btn fa-user"></i>Register
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

推荐答案

<select class="form-control" id="instid" name="instid" required="required">
    <option value="" selected disabled>Choose an institution.</option>
    @foreach($institutions as $institute)
      <option value="{{ institute->id }}">$institution->name</option>
    @endforeach
</select>

现在在您的控制器中:$request->input('instid');

Now in your controller: $request->input('instid');

public function store(Request $request)
{
   $instituteId = $request->input('instid');
}

这篇关于将选择字段添加到Bootstrap表单并将其发布到Laravel项目中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆