Skip to content
Advertisement

Send file with jquery and recive in PHP (No plugIn)

I’m trying to send a file using AJAX with the form but the PHP file seems to receive nothing, not only the files but also the form elements. In PHP if i try to get a value from the $_POST i get an Undefined Index. I’ve tried:

JavaScript

and the method you see in the code below. I’m restricted to do this without plugins. I was also trying with the $.post() but if i understood correctly i have to use $.ajax() to add the processData: false to send files.

HTML

JavaScript

JS

JavaScript

PHP

JavaScript

Advertisement

Answer

You should create a new FormData() and append your form values and files to it, and then send it as ‘multipart/form-data’ in the data (not body) param.

JavaScript

In PHP Your files will be avaiable in $_FILES array, and the other data will be in $_POST array.

More on FormData: https://developer.mozilla.org/en-US/docs/Web/API/FormData

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement