pratikzajam's avatar

Page is not getting redirected . i am not getting the issue.

public function store(Request $request) { $this->validate($request, [ 'title' => 'required|max:255', 'pdf_path' => 'required|file|mimes:pdf|max:2048', 'order_by' => 'nullable|integer' ]);

    $title = $request->input('title');
    $title_slug = Str::slug($title);
    $order_by = $request->input('order_by');

    $pdf_directory = "uploads/csr_policy";
    // Check if the directory already exists.
    if (!is_dir($pdf_directory)) {
        // Directory does not exist, so let's create it.
        mkdir($pdf_directory, 0755, true);
    }

    $pdf_path = $request->file('pdf_path')->storeAs(
        $pdf_directory,
        $title_slug . '.pdf'
    );

    // Reassign order numbers for CSR policies
    if ($order_by) {
        // Decrement order numbers greater than or equal to the new "order_by"
        csr_policy::where('order_by', '>=', $order_by)->increment('order_by');
    }

    $csr_policy = csr_policy::create([
        'title' => $title,
        'pdf_path' => $pdf_path, // Store the file path in the database
        'order_by' => $order_by
    ]);


    Log::info('File uploaded and CSR policy record created successfully.');


    Session::flash('success', 'CSR Policy Saved successfully');

    return redirect()->route('admin.blogs');
}

also my jquery code

    $(document).ready(function() {
        var form = $('#annualForm'); // Define the form variable here

        // Handle form submission on button click
        form.on('submit', function(event) {
            event.preventDefault(); // Prevent the default form submission

            // Clear previous error messages
            form.find('span.error').text('');

            // Create a FormData object to handle file uploads
            var formData = new FormData(this);

            // Define the URL to which the form data will be submitted
            var url = "{{ route('admin.csr_policies.store') }}"; // Update with the correct route name


            // Perform an Ajax POST request
            $.ajax({
                type: 'POST',
                url: url,
                data: formData,
                contentType: false, // Set to false to let jQuery handle the content type
                processData: false, // Set to false to prevent jQuery from processing the data
                beforeSend: function() {
                    $(".dis").prop('disabled', true); // disable button

                },

                success: function(response) {
                    $(".dis").prop('disabled', false);
            

                },
                error: function(error) {
                    $(".dis").prop('disabled', false);
                    var errors = jQuery.parseJSON(error.responseText);
                    $.each(errors.errors, function(k, v) {
                        var errorEle = form.find('span[data-name="' + k + '"]');
                        errorEle.text(v)
                    });
                }
            });
        });
    });
0 likes
4 replies
pratikzajam's avatar

@jlrdw thank u buddy . in my project on other other pages they have implemented redirection using php only. thats why was also doing the same. but it is not working . so i will redirect using JavaScript.

DhPandya's avatar

@pratikzajam While using an AJAX you must have to control the redirection from the success block of the AJAX. try using window.location in your success block.

window.location = "url".

Set a JSON return type so and remove redirection from your Controller's functions.

1 like

Please or to participate in this conversation.