使用下拉菜单从jquery获取数据时出错

问题描述 投票:0回答:1

`我想创建一个页面来标记学生的出勤情况,在该页面中我只需选择任何学生姓名,并在其他下拉列表中选择其学期、批次和学位

** 这是我的刀片视图代码,刀片视图中是否有任何错误**

                    @csrf
                    <select class="form-control mb-4" id="studentSelect">
                        <option value="">Select Student</option>
                        @foreach ($students as $student)
                            <option value="{{ $student->Student_id }}">{{ $student->Name }}</option>
                        @endforeach
                    </select>
                    <select name="batch_id"class="form-control mb-4" required>
                        <option value="" selected disabled>-- Select Batch --</option>
                        <option value="" id="batch"></option>
                    </select>
                    <select name="semester_id" class="form-control mb-4" required>
                        <option value="" selected disabled>-- Select Semester --</option>
                        <option value="" id="semester"></option>
                    </select>
                    <select name="degree_id" class="form-control mb-4" required>
                        <option value="" selected disabled>-- Select Degree --</option>
                        <option value="" id="degree"></option>
                    </select>
                </form>
                <script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4="
                    crossorigin="anonymous"></script>
                <script>
                    $('#studentSelect').on('change', function() {
                        var studentId = $(this).val();
                        $.ajax({
                            type: 'POST',
                            url: '/getStudentDetails',
                            data: {
                                '_token': '{{ csrf_token() }}',
                                'Student_id': studentId
                            },
                            success: function(data) {
                                $('#semester').val(data.semester);
                                $('#batch').val(data.batch);
                                $('#degree').val(data.degree);
                            },
                            error: function(xhr, status, error) {
                                console.error(xhr.responseText);
                                // Handle the error appropriately, e.g., display an error message
                            }
                        });
                    });
                </script>

这是出席者的控制器代码

<?php
namespace App\Http\Controllers;
use App\Models\batch;
use App\Models\degrees;
use App\Models\semester;
use App\Models\students;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class attendanceController extends Controller
{
    function showAttendencePage() {
        if(Session::has('user')){
            $students = students::all();
            return view('Pages.facuilty.markAttendence',['students'=>$students]);
        }else{
            return view('index');
        }
    }
    public function getStudentDetails(Request $request)
{    $student = students::find($request->Student_id);
    if ($student) {
        return response()->json([
            'semester' => $student->Semester_id,
            'batch' => $student->Batch_id,
            'degree' => $student->Degree_id,
        ]);
    }
    return response()->json(['error' => 'Student not found']);
}
}

*

*这是网络路线**

Route::group(['middleware' => ['web']], function () {
    Route::get('/mark/attendence/student',[attendanceController::class,'showAttendencePage'])->name('showAttendencePage');
    Route::post('/getStudentDetails', [attendanceController::class,'getStudentDetails'])->name('getStudentDetails');
});

这就是我的代码,请告诉我是否有任何错误或我无法解决的任何错误,所以请告诉我

php jquery mysql ajax laravel
1个回答
0
投票
$('#studentSelect').on('change', function() {
    var studentId = $(this).val();
    $.ajax({
        type: 'POST',
        url: '/getStudentDetails',
        data: {
            '_token': '{{ csrf_token() }}',
            'Student_id': studentId
        },
        success: function(data) {
            // Make Other ID's like this
            $('#semester').attr('selected','selected');
            $('#semester').html(data.semester);

            $('#batch').val(data.batch);
            $('#degree').val(data.degree);
        },
        error: function(xhr, status, error) {
            console.error(xhr.responseText);
            // Handle the error appropriately, e.g., display an error message
        }
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.