简体   繁体   中英

Sending id from ajax to controller for deleting data

I am getting some users data from database through Ajax. I added some checkboxes to the table for deleting data. I write code for that but I don't know how to send user id from the ajax to the controller.It would be nice if someone will help me. Below is my code

//Javascript

$(document).ready(function() {

            $(document).ready(function () {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
            });

            $('select[name="class_id"]').on('change', function() {
                var classID = $(this).val();
                if(classID) {

                    $.ajax({

                        url: '/attendance/ajax/'+classID,
                        type: "GET",
                        dataType: "json",
                        success:function(data) {

                            var markup = '';
                        markup += '<tr><th style="width: 2%" class="align-middle text-center"><input type="checkbox" id="options"></th><th style="width: 2%" class="align-middle text-center">#</th> <th style="width: 15%" class="text-center">Student ID<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Student Name<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Attendance<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Date<input type="text" class="form-control" disabled></th> <th style="width: 15%;" class="align-middle text-center">Actions</th> <tr>';

                        $.each(data, function(key, value) {

                            markup += '<tr> <td><input class="checkBoxes" type="checkbox" name="checkBoxArray[]"></td> <td><input type="hidden" value="'+value.id+'" name="id[]">' + value.id + '</td> <td><input type="hidden" value="'+value.student_id+'" name="student_id[]">' + value.student_id + '</td> <td><input type="hidden" value="'+value.first_name+'" name="first_name[]"><input type="hidden" value="'+value.last_name+'" name="last_name[]">' + value.first_name+ ' '  + value.last_name +  '<td><input type="hidden" value="'+value.attendance+'" name="attendance[]">' + value.attendance + '</td>' +  '<td><input type="hidden" value="'+value.created_at+'" name="created_at[]">' + value.created_at + '</td>' + '<td style=" width=12%" class="text-center"> <a><button title="Edit" class="btn btn-outline-primary"><span class="fas fa-pencil-alt"></span></button></a> </td>' +  '</td> <tr>';

                        });
                            $('table[id="studentsData"]').html(markup);
                        }
                    });
                }
            });
        });

//Submit button for deleting

<form method="post" action="delete/attendance" class="form-inline">

            {{csrf_field()}}

            {{method_field('delete')}}



            <div class="form-group">
                <select name="checkBoxArray" id="" class="form-control">

                    <option value="">Delete</option>

                </select>
            </div>

            <div class="form-group">

                <input type="submit" name="delete_all" class="btn btn-primary">

            </div>
        </form>

//Route

Route::delete('/students/delete/attendance', 'AttendanceController@deleteAttendance');

//Controller

 public function deleteAttendance(Request $request, $id) {
        if ($request->delete_single) {
            $this->destroy($id);
        }
        if(isset($request->delete_all) && !empty($request->checkBoxArray)) {

            $attendances = StudentsAttendance::findOrFail($request->checkBoxArray);

            foreach($attendances as $attendance) {

                $attendance->delete();

            }

            return redirect()->back();

        } else {

            return redirect()->back();

        }
    }

Since you want to send the set of ids to your action, create a POST route first :

Route::post('/students/delete/attendance', 'AttendanceController@deleteAttendance');

Then you must to attach the id with every checkbox like :

<input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="'+value.id+'">

And update you form action to :

<form method="post" action="/students/delete/attendance" class="form-inline">

Give it a go and check if you're getting all the selected id's in your deleteAttendance action by adding dd() at the top :

public function deleteAttendance(Request $request, $id) {
    dd( $request->checkBoxArray );

    ...
}

Your ajax url is diffrent from your route.

ajax : /attendance/ajax/{id}
route : /students/delete/attendance

if we follow same instruction from controller, it's need id, so change the ajax url to : '/students/delete/attendance/' +classID

Your ajax type is wrong, change GET to DELETE (same as route).

then your route is not use ID, but your pass url to route and controller with id.

Route::delete('/students/delete/attendance/{id}', 'AttendanceController@deleteAttendance');

on your controller just do :

public function deleteAttendance(Request $request, $id) {
    $query = StudentsAttendance::findOrFail($id);
    $query->delete();
}

if you want delete multiple student, it will be diffrent code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM