Drupal 7 FAPI - ajax image preview

僤鯓⒐⒋嵵緔 提交于 2019-12-21 06:38:56

问题


I am trying to preview the uploaded image using ajax in my D7 module but don't know why it's not working. Here is what I am trying to do:-

function create_ad_form($form, &$form_state)
{
   $form['image_file'] = array(
       '#title' => t('Upload Banner:'),
       '#type' => 'file',
   );

   $form['submit'] = array(
       '#type' => 'submit',
       '#value' => 'Submit',
       '#submit' => array('create_ad_form_submit'),
       '#ajax'=> array(
          'callback'=>'create_ad_form_submit',
          'wrapper'=> 'im-area',
          'method'=> 'replace',
          'effect'=> 'fade',
       )
   );

   $form['im-container']=array(
       '#prefix'=>'<div id="im-area">',
       '#suffix'=>'</div>',
   );
}

function create_ad_form_submit($form, $form_state)
{
   $file = file_save_upload('image_file', array(), "public://",$replace = FILE_EXISTS_REPLACE);
   if ($file)
   {
       $file->status=FILE_STATUS_PERMANENT;
       file_save($file);
       $form['im-container']=array(
             '#title'=>t('Preview:'),
             '#prefix'=>'<div id="im-area">',
             '#markup'=>'<img src="sites/default/files/'.$file->filename.'">',
             '#suffix'=>'</div>',
       );
   }
   else
       drupal_set_message('No file uploaded.');

   return $form['im-container'];
}

The above code gives no error but the image preview doesn't show up. Can anybody tell what I am doing wrong here and what should I do??? Thanks.


回答1:


OK. I got it working. I've updated the code above, its working for me. Just disabled and re-enabled the module and the same code started working. Its strange but it is, don't know what is the problem.



来源:https://stackoverflow.com/questions/13011522/drupal-7-fapi-ajax-image-preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!