Drupal 7 FAPI - ajax image preview(Drupal 7 FAPI - ajax 图像预览)
问题描述
我正在尝试在我的 D7 模块中使用 ajax 预览上传的图像,但不知道为什么它不起作用.这是我想要做的:-
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.
推荐答案
好的.我让它工作了.我已经更新了上面的代码,它对我有用.刚刚禁用并重新启用模块,相同的代码开始工作.很奇怪,但不知道是什么问题.
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.
这篇关于Drupal 7 FAPI - ajax 图像预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Drupal 7 FAPI - ajax 图像预览
				
        
 
            
        基础教程推荐
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
 - php 7.4 在写入变量中的 Twig 问题 2022-01-01
 - php中的foreach复选框POST 2021-01-01
 - 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
 - php中的PDF导出 2022-01-01
 - Web 服务器如何处理请求? 2021-01-01
 - 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
 - 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
 - Yii2 - 在运行时设置邮件传输参数 2022-01-01
 - 将变量从树枝传递给 js 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				