0

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.

1 Answer 1

0

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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