Skip to content
Advertisement

appending array to FormData and send via AJAX

I’m using ajax to submit a multipart form with array, text fields and files.

I append each VAR to the main data as so

JavaScript

then I use the ajax function to send it to a PHP file to store inside sql DB.

JavaScript

But on the PHP side, the arr variable, which is an array appears as a string.

When I don’t send it with ajax as Form data but use the simple $.POST option I do get it as an array on the PHP side, but then I can’t send the files as well.

any solutions?

Advertisement

Answer

You have several options:

Convert it to a JSON string, then parse it in PHP (recommended)

JS

JavaScript

PHP

JavaScript

Or use @Curios’s method

Sending an array via FormData.


Not recommended: Serialize the data with, then deserialize in PHP

JS

JavaScript

PHP

JavaScript
Advertisement