php message Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

php message Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

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

Stefan Bogdanescu

Founder & Senior Architect

php message Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

The error Warning: Missing boundary in multipart/form-data POST data is a classic symptom of a mismatch between how your JavaScript constructs the HTTP request and how your PHP backend expects to parse it. In this specific scenario, the root cause lies in the conflicting configuration of your XMLHttpRequest object within the ajax_post() function.

You are setting the header to multipart/form-data:

hr.setRequestHeader("Content-type", "multipart/form-data");

However, immediately after, you instantiate a FormData object and pass it to hr.send(formData). While FormData is excellent for gathering binary files and named fields from a DOM form, it automatically sets the Content-Type header to multipart/form-data; boundary=----WebKitFormBoundary.... The boundary string changes every time a request is sent.

By manually calling setRequestHeader("Content-type", "multipart/form-data") without specifying a boundary, you overwrite the automatic header set by the browser's FormData API with a generic version that lacks the required boundary parameter. When PHP's default $_POST parser receives this malformed multipart stream, it cannot locate the separator string needed to distinguish between fields, resulting in the "Missing boundary" warning and an empty $_POST array.

Correcting the JavaScript Request

To fix this, you must allow the browser to manage the Content-Type header entirely. The FormData object is designed to handle the complexity of multipart encoding, including generating a unique boundary string for each request. You should remove the manual setRequestHeader call if you are sending a FormData instance.

Here is the corrected logic for your ajax_post function:

function ajax_post() {
    var url = "LiveUpdate.php";
    
    // Correctly gather data from the form elements directly
    var sb = document.getElementById("LiveUpdate").value;
    var FirstName = document.getElementById("FirstName").value;
    var images = document.getElementById("images");

    var formData = new FormData(images.form); // Ensure we grab the parent form or specific inputs
    
    // CRITICAL FIX: Do NOT set Content-Type manually when using FormData.
    // The browser will handle the boundary automatically.
    
    hr.open("POST", url, true);
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("message").innerHTML = return_data;
        }
    };

    // Send the FormData object directly
    hr.send(formData); 
}

Note that in your original snippet, you mixed manual variable string concatenation (var vars = "update="+sb...) with FormData. If you construct a string manually, you must set headers for application/x-www-form-urlencoded and use hr.send(vars). If you use FormData, you must not send a string and must not manually set the Content-Type header. Mixing these two approaches guarantees failure.

Handling the Form Action Attribute

There is another architectural issue in your HTML: the <form> tag has action="popup.php", but your JavaScript sends data to "LiveUpdate.php". While this won't cause the boundary error directly, it indicates a disconnect in your routing logic. If you are performing an AJAX update without page reloads, the action attribute of the form is irrelevant because you are bypassing the standard form submission mechanism entirely.

Ensure that your target PHP file (LiveUpdate.php) is configured to handle both $_POST and potentially JSON if you decide to refactor later. If you switch to sending JSON (which avoids the boundary issue entirely), you would change the header to:

hr.setRequestHeader("Content-Type", "application/json");
// And send JSON.stringify(formData) or a plain object

Server-Side Considerations

On the PHP side, the error occurs because file_get_contents('php://input') is not being used. If your script relies on $_POST to capture files and text fields in a multipart request, you must ensure the boundary is present. The most robust approach for handling file uploads in PHP is often using $_FILES.

If you switch to JSON for better performance and cleaner code (avoiding boundaries altogether), update your PHP handler to decode the JSON payload:

$input = file_get_contents('php://input');
$data = json_decode($input, true);

if ($data) {
    // Process $data['FirstName']
}

This approach eliminates the boundary dependency entirely, as JSON does not rely on multipart delimiters. It is generally preferred for AJAX applications unless you specifically require native multipart file handling for binary streams without serialization.

Our network