Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DewiH's avatar
Level 1

How to display ajax result in table when dropdown is changed

Hello,

I'd like to ask help please : in my blade I have a dropdown menu of club list; what I'd like to have is, when a club is selected from this dropdown, I'll have the club's members shown in the table below, where I can check or not (club member attendance). The ajax script is working, but I don't know how to display the ajax result in the table. (I hope my explanation is not too confusing)..

<form method="POST" action="{{ route("admin.club-attendances.store") }}" enctype="multipart/form-data">
            @csrf
            <div class="row">
                <div class="col">
                    <div class="form-group">
                        <label for="club_id">{{ trans('Club') }}</label>
                        <select class="form-control select2 " name="club_id" id="club_id">
                            @foreach($clubs as $id => $entry)
                                <option value="{{ $id }}" {{ old('club_id') == $id ? 'selected' : '' }}>{{ $entry }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="col">
                    <div class="form-group">
                        <label for="attendance_date">{{ trans('Attendance Date') }}</label>
                        <input class="form-control date" type="text" name="attendance_date" id="attendance_date" value="{{ old('attendance_date') }}">
                    </div>
                </div>
            </div>
<div class="row">
                <div class="col-sm-12">
                    <table class="table table-bordered dataTable">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>Student</th>
                            <th>Present</th>

                        </tr>
                        </thead>
                        <tbody>
                            @foreach(..... as $member)
                                <tr>
                                    <td class="sorting_1">{{$loop->iteration}}</td>
                                    <td>{{ $member->full_name ?? ''}}</td>
                                    <td>
                                        <input type="hidden" name="present" value="0">
                                        <input class="form-check-input" type="checkbox" name="present" id="present" value="1" {{ old('present', 0) == 1 || old('present') === null ? 'checked' : '' }}>    
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
            
            <div class="form-group">
                <button class="btn btn-danger" type="submit">
                    {{ trans('Save') }}
                </button>
            </div>
</form>

<script>
    $(document).ready(function(){
        $('#club_id').on('change', function(){
            var clubID = $(this).val();
            console.log(clubID);

            $.ajax({
                type: "GET",
                url: "/getClubMembers/" + clubID,
                dataType: 'json',
                success: function(data){
                    console.log(data);
                }
            });
        });
    });
</script>

In my route web.php :

Route::get('/getClubMembers/{id}',function($id){    // $id is the Club ID
    $members = Student::whereHas('studentClubRegistrations', function ($q) use($id) {
        $q->where('status_id',102)->whereHas('clubs', function($qq) use($id){
            $qq->where('club_activity_id', $id);
        });
    })->get()->pluck('full_name', 'id');
    return response()->json($members);
});
0 likes
6 replies
Tray2's avatar

You get a json response from the ajax call I take it?

If so you can in regular js do something like.

let tableHTML = '';
response.forEach((item) => {
  tableHTML += `<tr><td>${item.member_no}</td><td>${item.name}`;
});

document.querySelector('#member-list').innerHTML = tableHTML;
1 like
DewiH's avatar
Level 1

@Tray2 Thank you for the idea !! After several trials, here's the current working code, though it looks crude.

<div class="row">
                <div class="col-sm-12">
                    <table id="datatable" class="table table-bordered dataTable table-responsive-lg">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>Student</th>
                            <th>Presence</th>

                        </tr>
                        </thead>
                        <tbody id="listMembers">

                        </tbody>
                    </table>
                </div>
            </div>


<script>
    $(document).ready(function(){
        $('#club_id').on('change', function(){
            var clubID = $(this).val();
            console.log(clubID);

            $.ajax({
                type: "GET",
                url: "/getClubMembers/" + clubID,
                dataType: 'json',
                success: function(data){
                    console.log(data);
                    $("#listMembers").empty();
                    row=1;
                    $.each(data,function(index,value){
                        $("#listMembers").append('<tr><td>' + row++ +'</td><td><input class="form-control" type="text" id="student_id" name="student_id" value="' + value +'" disabled></td><td >'+
                            '<input class="" type="checkbox" name="present" id="present" value="1" {{ old("present", 0) == 1 ? "checked" : '' }}>'    +
                            '<input type="hidden" name="present" value="0">' +
                            '</td></tr>');
                    });
                }
            });
        });
    });
</script>
Tray2's avatar

@DewiH You really should use template strings, it makes the code more readable compared to contatination.

Oh, and why are you using a form inside the row?

DewiH's avatar
Level 1

@Tray2 Actually I'm trying to make an attendance system : by selecting the club from the dropdown, the club's member will be automatically shown, then we can check their presence by checking the checkbox.

But now, when I'm trying to store that form, only the last row is saved ...

Tray2's avatar
Tray2
Best Answer
Level 73

@DewiH Change the name="student_id" to name="student_id[]" Then you can loop through the result serverside.

1 like

Please or to participate in this conversation.