lewanay
7 months ago

How to store json data

Posted 7 months ago by lewanay

Hello community I am retrieving json data and now from the same page i want to go to the store action and wanna save it so i placed href on the save button and then write some code in javscript for button action.. Now when i have no data and press the save button then it do nothing but when i have some data and click the save button then it give me 409 page in the console .. Below is my code

//My route

Route::get('/students/attendance', '[email protected]');

//My javascript

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

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

                        var attendance = `<div class="form-group">
                    <select class="form-control" id="attendance" name="attendance[]">
                        <option>Present</option>
                        <option>Absent</option>
                        <option>Leave</option>
                    </select>
                    </div>`;

                        var markup = '';
                        markup += '<tr> <th>#</th> <th>Student ID</th> <th>Student Name</th> <th>Attendance</th> <tr>';

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

                             markup += '<tr> <td><input type="hidden" value="value.id" name="id[]">' + value.id + '</td> <td>' + value.student_id + '</td> <td>' + value.first_name+ ' '  + value.last_name  + '</td> <td> ' + attendance +  '</td> <tr>';

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


                        $('body').on('click', '#save-btn', function(e){
                            e.preventDefault();

                            var data = $('#studentsData').find('select, input').serialize();

                            $.post('/students/attendance', {data: data}, function(response){
                                console.log(response);
                            });
                        });
                    }
                });
            }
        });
    });

</script>

//View

    <a href="{{\Illuminate\Support\Facades\URL::to('/students/attendance')}}" id="save-btn" class="fas fa-folder-open btn btn-success float-right mb-4 mr-2">
        Save
    </a>

    </div>

Please sign in or create an account to participate in this conversation.