PhoeniX5
1 month ago
353
8
Laravel

Laravel prevent form resubmission on page refresh

Posted 1 month ago by PhoeniX5

I am using ajax to submit data in MySQL tables and display it in another view, the problem is that when refreshing the second view (F5 / CTRL + R) it resubmits all the data, which creates unwanted dupes in MySQL tables, what is the best way to prevent this from happening ?

Here is my code :

View :

$('#aff_form').on('submit', function(event) {
                //event.preventDefault();
                var form_data = $(this).serialize();
                var material_code = $('#material_code').val();
                var personnel = $('#personnel').val();
                var fonction = $('#fonction').val();
                var service = $('#service').val();
                var user_id = $('#user_id').val();
                var remarque = $('#remarque').val();
                var note = $('#note').val();
                $.ajax({
                    url: "{{ route('Affectation.createAff') }}",
                    method: "POST",
                    data: {
                        material_code: material_code,
                        personnel: personnel,
                        fonction: fonction,
                        service: service,
                        remarque: remarque,
                        note: note,
                        user_id: user_id
                    },
                    success: function(data) {
...
}

Controller :

...
 return view('Mise_a_disposition', compact('nom', 'fonction', 'service', 'imei1', 'imei2', 'remarque', 'aff_code'), ['mat_aff' => $mat_aff]);
}

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