从Form Select检索输入-Laravel

时间:2018-06-27 20:08:17

标签: php laravel

当我从视图中选择一个类并单击Genrate时,我尝试检索所选类的值,但无法获取所选类的值。

当我返回$condition时,它返回空值。提交后的网址中,它显示为http://localhost:8000/generate/timetable?class=

为什么会这样?

<div class="row">
    <div class="col-xl-4 col-lg-12 col-md-12 mb-1">
        <fieldset class="form-group">
            <label for="squareText">Type</label>
            {{ Form::select('class',$classes,$selectedClass,['class'=>'form-control','required'=>'true'])}}
            <br>
            <a href="/generate/timetable?class={{ request('class') }}"><button class="btn btn-primary pull-left square"  type="submit"><i class="glyphicon glyphicon-th"></i> Generate</button></a>
        </fieldset>
    </div>
</div>

控制器

$condition = Input::get('class');
if (!isset($condition['class']))
    $condition['class'] = 0;
$courses = $course->where('class',$condition)->get()->toArray();
return $condition;

1 个答案:

答案 0 :(得分:0)

您的按钮在链接内:

<a href="/generate/timetable?class={{ request('class') }}">
    <button class="btn btn-primary pull-left square" type="submit"> ... </button>
</a>

单击它实际上是在单击<a href>链接,而不是单击按钮并提交表单。因此,您无需提交表单,而只需浏览链接所指向的URL。

从您的问题中尚不清楚链接的用途,但是如果应该单击按钮来提交表单,则您可能希望将表单元素放入实际的<form>中。

您似乎正在使用Laravel Collective({{ Form::select('class ....就是这样),因此请参见those docs on how to open a form

{!! Form::open(['url' => '/generate/timetable?class=' . request('class')]) !!}
    {{ Form::select('class' .... }}
    <button class="btn btn-primary pull-left square" type="submit"> ... </button>
{!! Form::close() !!}

probalby还需要添加一个CSRF令牌,它也是described in the Laravel Collective docsin the Laravel docs