You use window location since you are using ajax and JS.
https://developer.mozilla.org/en-US/docs/Web/API/Window/location
I'd just have a success div, and add a button to redirect.
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)
});
}
});
});
});
Please or to participate in this conversation.