Send FormData and String Data Together Through JQuery AJAX?

Send FormData and String Data Together Through JQuery AJAX?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Send FormData and String Data Together Through JQuery AJAX?

Combining file uploads with standard form fields in a single request is a common requirement, but mixing FormData objects with jQuery's default handling often leads to silent data loss. The core issue isn't that the browser can't handle both types of data; it's that the way you construct the FormData object and how jQuery serializes the rest of your form creates a conflict in the HTTP request body.

When you use $('form').serialize(), jQuery generates a URL-encoded string (e.g., page_id=100&category_id=12). However, when you append a file to a FormData object and pass that to $.ajax() with processData: false and contentType: false, the browser automatically switches the request to multipart/form-data. This content type expects all data to be structured as distinct parts within a MIME boundary. If you try to mix a raw URL-encoded string with binary file data in the same payload without explicit conversion, the server—likely configured to parse $_POST and $_FILES separately based on content type—will fail to deserialize the mixed format correctly.

In your specific scenario, the server receives the file because it was explicitly appended to the FormData object. The hidden inputs are missing because they were serialized into a URL-encoded string (page_id=&...) rather than being added as individual key-value pairs within the FormData instance itself. PHP interprets the request body based on the Content-Type header. When that header is set to multipart/form-data, PHP expects every field to be separated by a boundary marker. A raw string does not conform to this structure, resulting in an empty $_POST array while the file data remains intact.

To fix this, you must populate the FormData object with both the file and the hidden input fields before sending the request. This ensures that every piece of data shares the same multipart boundary, allowing PHP's default parser to handle both streams uniformly.

Here is how to correctly construct the request:

var fd = new FormData();

// Append the file
fd.append("file", object.get(0).files[i]);

// Append hidden inputs explicitly
fd.append("page_id", $('#page_id').val());
fd.append("category_id", $('#category_id').val());
fd.append("method", "upload");
fd.append("required[category_id]", "Category ID");

$.ajax({
    url: 'add.php',
    data: fd, // Pass the FormData object directly
    processData: false, // jQuery must not process this into a query string
    contentType: false, // Let the browser set multipart/form-data boundary
    type: 'POST',
    success: function(data){
        console.log(data);
    }
});

By appending each field individually to fd, you guarantee that they are serialized as distinct parts within the multipart stream. When this request reaches add.php, PHP's internal logic will parse the boundary markers correctly, populating both $_FILES and $_POST with their respective data arrays. This approach eliminates the need for custom parsing logic on the server side and aligns with standard HTTP multipart specifications.

If you are working within a Laravel application or another framework that handles file uploads, ensure your controller methods explicitly check both request()->file() and request()->input(). While this solution resolves the immediate issue of data loss in jQuery AJAX requests, be mindful that large payloads containing multiple files and numerous fields can increase request size. In high-volume scenarios, consider whether a dedicated API endpoint for metadata (using JSON) alongside a separate upload endpoint might offer better performance tuning than combining everything into a single multipart request.

Our network