javascripthtmllaravel

Data not getting into the database


I am trying to send data into the database but unfortunately its returning null... The button with type="submit" is working well it is saving all the marks of the student at once but the button which is saving one subject at at time the one with "SaveSingleSubject" class is the one returning null on the fields, exam_work,class_work,test_work... what is the issue blade code

@if (!empty($getStudent) && !empty($getStudent->count()))
    @foreach ($getStudent as $student)
        <form name="post" class="SubmitForm">
            {{csrf_field()}}
            <input type="hidden" name="student_id" value="{{$student->id}}">
            <input type="hidden" name="exam_id" value="{{Request::get('exam_id')}}">
            <input type="hidden" name="class_id"
                   value="{{Request::get('class_id')}}">
            <tr>
                <td>{{$student->name}}{{$student->last_name}}</td>
                @php
                    $i = 1;
                @endphp
                @foreach ($getSubject as $subject)
                    @php
                        $getMark = $subject->getMark($student->id, Request::get('exam_id'), Request::get('class_id'), $subject->subject_id);
                    @endphp
                    <td>
                        <div style="margin-bottom: 10px;">
                            Class Work
                            <input type="hidden" name="mark[{{$i}}][subject_id]"
                                   value="{{$subject->subject_id}}" style="width: 200px;"
                                   class="form_control" placeholder="Enter Marks">
                            <input type="text" name="mark[{{$i}}][class_work]"
                                   id="class_work_{{$student->id}}{{$subject->id}}"
                                   style="width: 200px;" class="form_control"
                                   placeholder="Enter Marks"
                                   value="{{!empty($getMark->class_work) ? $getMark->class_work : ''}}">
                        </div>
                        <div style="margin-bottom: 10px;">
                            Home Work
                            <input type="text" name="mark[{{$i}}][home_work]"
                                   id="home_work_{{$student->id}}{{$subject->id}}"
                                   style="width: 200px;" class="form_control"
                                   placeholder="Enter Marks"
                                   value="{{!empty($getMark->home_work) ? $getMark->home_work : ''}}">
                        </div>
                        <div style="margin-bottom: 10px;">
                            Test Work
                            <input type="text" name="mark[{{$i}}][test_work]"
                                   id="test_work_{{$student->id}}{{$subject->id}}"
                                   style="width: 200px;" class="form_control"
                                   placeholder="Enter Marks"
                                   value="{{!empty($getMark->test_work) ? $getMark->test_work : ''}}">
                        </div>
                        <div style="margin-bottom: 10px;">
                            Exam Work
                            <input type="text" name="mark[{{$i}}][exam_work]"
                                   id="exam_work_{{$student->id}}{{$subject->id}}"
                                   style="width: 200px;" class="form_control"
                                   placeholder="Enter Marks"
                                   value="{{!empty($getMark->exam_work) ? $getMark->exam_work : ''}}">
                        </div>

                        <div style="margin-bottom: 10px;">
                            <button type="button"
                                    class="btn btn-primary SaveSingleSubject"
                                    id="{{$student->id}}"
                                    data-val="{{$subject->subject_id}}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "
                                    data-exam="{{Request::get('exam_id')}}"
                                    data-class="{{Request::get('class_id')}}">Save
                            </button>
                        </div>
                    </td>
                    @php
                        $i++;
                    @endphp
                @endforeach
                <td>
                    <button type="submit" class="btn btn-success">Save</button>
                </td>
            </tr>
        </form>
        @endforeach
        @endif


        </tbody>
        </table>
        </div>
        </div>
        @endif


        <script type="text/javascript">
            $('.SubmitForm').submit(function (e) {
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "{{url('admin/examinations/submit_marks_register')}}",
                    data: $(this).serialize(),
                    dataType: "json",
                    success: function (data) {
                        alert(data.message);
                    }
                });
            });

            $('.SaveSingleSubject').click(function (e) {
                var student_id = $(this).attr('id');
                var subject_id = $(this).attr('data-val');
                var exam_id = $(this).attr('data-exam');
                var class_id = $(this).attr('data-class');
                var class_work = $('#class_work_' + student_id + subject_id).val();
                var home_work = $('#home_work_' + student_id + subject_id).val();
                var test_work = $('#test_work_' + student_id + subject_id).val();
                var exam_work = $('#exam_work_' + student_id + subject_id).val();


                $.ajax({
                    type: "POST",
                    url: "{{url('admin/examinations/single_submit_marks_register')}}",
                    data: {
                        "_token": "{{csrf_token()}}",
                        student_id: student_id,
                        subject_id: subject_id,
                        exam_id: exam_id,
                        class_id: class_id,
                        class_work: class_work,
                        home_work: home_work,
                        test_work: test_work,
                        exam_work: exam_work,


                    },
                    dataType: "json",
                    success: function (data) {
                        alert(data.message);
                    }
                });
            });


        </script>
        </body>

        </html>

controller file-> this is the controller file

 public function single_submit_marks_register(Request $request)
    {

        \Log::info('Request Data:', $request->all());
        $class_work = !empty($request->class_work) ? $request->class_work : 0;
        $home_work = !empty($request->home_work) ? $request->home_work : 0;
        $test_work = !empty($request->test_work) ? $request->test_work : 0;
        $exam_work = !empty($request->exam_work) ? $request->exam_work : 0;

        $getMark = marksregistermodel::CheckAlreadyMark($request->student_id, $request->exam_id, $request->class_id, $request->subject_id);
        if (!empty($getMark)) {
            $save = $getMark;
        } else {
            $save = new marksregistermodel;
            $save->created_by = Auth::user()->id;
        }

        $save->student_id = $request->student_id;
        $save->exam_id = $request->exam_id;
        $save->class_id = $request->class_id;
        $save->subject_id = $request->subject_id;
        $save->class_work = $class_work;
        $save->home_work = $home_work;
        $save->test_work = $test_work;
        $save->exam_work = $exam_work;

        $save->save();

        $json['message'] = "Mark Register Successfully Saved";
        echo json_encode($json);
    }

error logs the following are the logs am recieving when I click the save button the class_id,subject_id,student_id are working fine but the rest are returning null

[2024-07-23 14:58:57] local.INFO: Request Data: {"_token":"NUxJAZgGntTF65oczF8elM3fudmIxTjYPqY5OyN4","student_id":"34","subject_id":"11","exam_id":"2","class_id":"4","class_work":null,"home_work":null,"test_work":null,"exam_work":null} 

Solution

  • difficult to tell what the problem is, but I think if you are trying to match the IDs of your inputs, here:

    <button type="button"
      class="btn btn-primary SaveSingleSubject"
      id="{{$student->id}}"
      data-val="{{$subject->subject_id}}"
      data-exam="{{Request::get('exam_id')}}"
      data-class="{{Request::get('class_id')}}">Save
    </button>
    

    data-val="{{$subject->subject_id}}" should be data-val="{{$subject->id}}". Why would a subject have a subject_id field, should it not be id like in your input? Did you check if this data-val is null?

    This way it should match the id="exam_work_{{$student->id}}{{$subject->id}}" in your input.

    Also another thing would be to split student id with subject id like:

    id="exam_work_{{$student->id}}_{{$subject->id}}"

    since if you don't you may have for example a student with ID=12 and subject ID=15, produce the same id with a student with ID=121 and subject ID=5.