Angularjs - simple form submit

Angularjs - simple form submit

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

Stefan Bogdanescu

Founder & Senior Architect

AngularJS Form Submission: Handling Mixed Content and File Uploads

Building a clean, standard form in AngularJS is straightforward, but bridging that client-side model to a server often introduces friction, especially when dealing with mixed content types like text and binary files. The core challenge isn't just binding inputs; it's constructing the correct HTTP payload for your backend.

In your specific scenario, you are mixing standard form elements with file inputs. This distinction dictates how your ng-submit handler behaves. AngularJS provides a built-in abstraction called $http, but relying solely on its default JSON serialization breaks when you need to send files. Browsers do not serialize file objects into JSON strings; they expect multipart-encoded streams for that data type.

Handling the Payload and PHP Interaction

Your controller currently attempts to log formData after assignment. To understand how your PHP file interacts with this, you must first decide on the transmission strategy. If you stick with standard form tags (like <input type="text">), AngularJS will automatically serialize the model into a URL-encoded string by default if you use FormData. However, to send files correctly, you generally need to switch your submission method.

Instead of sending a raw JSON string via JSON.stringify, which cannot contain file objects, you should utilize the FormData API in JavaScript. This object automatically handles the boundary parameters required by PHP's $_FILES and $_POST.

Here is how the submission logic should look in your controller:

$scope.submitForm = function() {
    // Create a FormData object from the scope model
    var formData = new FormData();
    
    // Map each field from the Angular model to the FormData object
    for (var key in $scope.form) {
        if ($scope.form.hasOwnProperty(key)) {
            formData.append(key, $scope.form[key]);
        }
    }

    console.log("posting data....");
    
    $http.post('form.php', formData, {
        transformRequest: angular.identity, // Prevents JSON transformation for files
        headers: { 'Content-Type': undefined } // Allows the browser to set the multipart boundary
    }).success(function(response) {
        console.log("Server response:", response);
    });
};

On the PHP side, you no longer expect a JSON string. Instead, $_POST will contain your text fields as usual, and $_FILES will contain an array of uploaded files. You can iterate through these arrays to process images or other documents directly on the server without needing intermediate file I/O in JavaScript.

Checkbox Logic and Boolean Values

Regarding your question on checkbox values: AngularJS handles booleans elegantly when using ng-model. If your HTML is <input type="checkbox" ng-model="form.member">, Angular binds this to a boolean true or false.

However, in your source code, you defined the value as value="5" and initialized the model as member: false. This creates a mismatch. When a checkbox is unchecked, Angular sets it to false. If checked, it becomes true. You do not need to manually append strings for these values unless you are sending them via JSON and want to control the serialization string explicitly. Ensure your PHP logic checks for truthiness (isset or checking if the value is not empty) rather than expecting a specific string like "5" unless that is your explicit business requirement.

File Upload Considerations

When submitting images, as seen with file_profile and file_avatar, the primary consideration is the HTTP boundary. As shown in the code above, passing FormData to $http.post combined with { transformRequest: angular.identity } is critical. If you omit this configuration or use JSON.stringify, Angular will attempt to convert the file object to a string, resulting in data corruption on the server.

You must ensure your PHP script expects multipart/form-data. The browser automatically handles setting the correct header and boundary when using the FormData object with $http. This approach keeps your JavaScript code clean while ensuring the server receives binary data intact. If you were to mix jQuery AJAX with Angular, you would have to manually configure these headers, which is a common pitfall for beginners migrating between libraries. Sticking to native AngularJS directives like ng-submit and $http ensures consistency in how the payload is constructed and sent.

Our network