With AJAX you can perform an action on the server without refreshing the whole page.

You can use it to  fetch data,  import file data,  upload files, etc.

In this tutorial, I am using it to upload a file and display a  preview if the file is an image otherwise display a link in the CodeIgniter 4 project.

I have enabled CSRF token in the CodeIgniter project.

Contents

1. Enable CSRF

  • Open .env file.

2. Route

  • Open app/Config/Routes.php file.

3. Controller

  • Create UsersController.php file in app/Controllers/ folder.

4. View

Create index.php file in app/Views/.

5. Output

6. Conclusion

Pass selected file using FormData Object to upload in AJAX request.

#ajax #codeigniter 4 #jquery #php

How to upload a file using jQuery AJAX in CodeIgniter 4
3.80 GEEK