I am using Drupal Form API to upload profile image for user. I found two file upload widgets in FORM API:

  1. '#type' => 'managed_file' &
  2. '#type' => 'file'

managed_file requires first selecting the file, then uploading the file and then submitting the form to change the image

file requires first selecting the file and then submitting the form to change the image

What I want to do is just when you select the image file and click ok in the file browser, the form should get submitted and image should be changed.

Is it possible using FORM API? If not please recommend an alternative solution.


Of course it is possible. You need to take regular managed file and add some JavaScript to the file selection in it. To see how it is done, please see AutoUpload module:

This module removes the extra button press and hides the "Upload" button via JavaScript for a quicker, automatic file upload. When JavaScript is not enabled, it falls back to the "Upload" button.

The most interesting is line 9, out of it's 22 lines of code:

$('.form-item input.form-file', context).change(function() {

And maybe simply using this module is all you really need?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.