2

I make a form using FAPI, and this form contains a file managed field. I want to show an alert after uploading the file ("uploaded successfully"), and a confirmation ("Are you sure you want to delete file") when deleting the file.

This my code.

$form['my_file_fid'] = array(
  '#type' => 'managed_file',
  '#title' => t('Files'),
  '#default_value' => variable_get('my_file_fid', ''),
  '#upload_location' => 'public://',
); 

I tried to add #file_value_callbacks, but I don't know how to use it.

I tried to make a javascript just to alert when I click upload,remove button but it doen not work I tried to make it general to all input to check that js is correct now all inputs are clickable but upload and remove button not

jQuery(document).ready(function() {
         jQuery('input').bind('click',function() { alert("hi") });
 });  

I make this js to check jquery ajaxSuccess function

2 Answers 2

0

You could just bind something to the jQuery ajax success event on the page where your form is displayed.

You can bind ajaxSuccess to any jQuery object, try something like this if you're experimenting:

<script>
  (function ($) {
    $(function () {
      jQueryObject = $({});
      jQueryObject.ajaxSuccess(function(event, XMLHttpRequest, ajaxOptions) {
        /* do something whenever an ajax call is successful */
      });
    });
  })(jQuery);
</script>
6
  • I don't know how,file_manged field have upload button by default and its have ajax function how can I bind something to it and i don't know where the success function
    – Ahmed
    Commented Jul 29, 2012 at 9:18
  • I can't teach you how to use jQuery and bind functions to events in javascript but the documentation for the function I'm talking about is here - api.jquery.com/ajaxSuccess Commented Jul 29, 2012 at 9:24
  • it is not a problem to bind jQuery event to a control I tried bind click to upload button but it does not accept any thing i tried this jQuery(document).ready(function() { jQuery('input').bind('click',function() { alert("hi") }); }); all input in the form have alert except the upload and remove button
    – Ahmed
    Commented Jul 29, 2012 at 10:08
  • @Ahmed Try editing your question with the jquery that you tried. It will add some clarification to some of the things you have done in search of the answer. It will also bump up your question giving it a better chance to be answered. Commented Jul 29, 2012 at 23:00
  • The upload form is being changed via AJAX isn't it? That means if you bind an event to that element, you lose the binding when the element is overwritten. I'm suggesting that you bind something to the ajaxSuccess event which happens for the whole document. I'll update my response with an example. Commented Jul 31, 2012 at 2:57
0

Hope this will help, use hook_file_presave inside that you can use drupal set message.

/**
 * implementing hook_node_presave 
 */
function mymodule_file_presave($file) {
//dpm($file);
// 
 if (isset($file->source) && $file->source=='field_preview_image_file_und_0')//make condition like this for ur file
{
//use the drupal set message or java script alert  on successful upload
drupal_add_js(drupal_get_path('module','mymodule') . '/test.js');
}
}

create a js file on same module folder as test.js

(function ($) {
$(document).ready(function(){
alert('this is alert message');  
});
})(jQuery);

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.