如何获取选定的数据值到路由?

时间:2019-07-14 12:07:58

标签: javascript php laravel laravel-5 drop-down-menu

大家好,我不熟悉Stack并首次发布。 我正在为学校建立一个考勤管理系统。在我的注册和出勤表中,我有

Registration : class_id, section_id, user_id.

Attendance :  date, user_id, remarks.

我希望用户选择班级,然后选择节和日期,然后通过注册表显示该班级和节的所有用户。

在将所选数据发送到路由时遇到问题。我已经试过了,我希望数据从下拉菜单变为这种形式

<a href="{{url('/registration/data/'.$classs->class_id .$classs->section_id)}}">

PHP页面

@php use Illuminate\Support\Facades\DB;
$classs = DB::table("classses")->pluck("title","id");
$registrations= DB::table("registrations")->pluck("class_id","section_id");


@endphp



        <div class="form-group row">

            <label for="title" class="col-md-4 col-form-label">Select Class:*</label>

            <div class="col-md-4 col-form-label">
                <select name="class_id" class="form-control" style="width:360px" id="class_id" required autofocus>
                    <option value="">--- Select Class ---</option>
                    @foreach ($classs as $key => $value)
                        <option  value="{{ $key}}">{{ $value }}</option>
                    @endforeach
                </select>

            </div>
        </div>
        <div class="form-group row">
            <label for="title" class="col-md-4 col-form-label">Select Section:*</label>
            <div class="col-md-4 col-form-label">
                <select name="section_id" class="form-control" style="width:360px" required autofocus>
                    <option value="">--- Select Section --- </option >
                </select>
            </div>

        </div>



        <a href="{{url('/registration/data/'.$classs->class_id .$classs->section_id)}}">

            <div class="form-group " align="right">

            <button type="submit" class="btn btn-primary">

                {{ __('Submit') }}
            </button>

        </div>
        </a>

        <script type="text/javascript">
            $(document).ready(function() {
                $('select[name="class_id"]').on('change', function() {
                    var classID = $(this).val();
                    if(classID) {
                        $.ajax({
                            url: '/myform/ajax/'+classID,
                            type: "GET",
                            dataType: "json",
                            success:function(data) {


                                $('select[name="section_id"]').empty();
                                $.each(data, function(key, value) {
                                    $('select[name="section_id"]').append('<option value="'+ key +'">'+ value +'</option>');
                                });

                            }
                        });
                    }else{
                        $('select[name="section_id"]').append('<option >No Section Found</option>');
                    }
                });
            });
        </script>


    </div>
</div>
</div>

尝试从下拉菜单中获取选定数据的ID主要存在问题:

   <a href="{{url('/registration/data/'.$classs->class_id .$classs->section_id)}}">

            <div class="form-group " align="right">

            <button type="submit" class="btn btn-primary">

                {{ __('Submit') }}
            </button>

        </div>
        </a>

想以这种形式在路线中转移ID

 Route::any('/registration/data/{class_id}/{section_id}','AttendanceController@getUsers');

2 个答案:

答案 0 :(得分:0)

我认为您只是在网址中缺少斜线:

<a href="{{url('/registration/data/'.$classs->class_id .$classs->section_id)}}">

应该是:

<a href="{{url('/registration/data/'.$classs->class_id .'/' .$classs->section_id)}}">

答案 1 :(得分:0)

我想在这里您在class_id和section_id之间省略了斜杠:

{{url('/registration/data/'.$classs->class_id .$classs->section_id)}}