Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:10:27+00:00 2026-06-13T13:10:27+00:00

I am trying to preview the uploaded image using ajax in my D7 module

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:10:28+00:00Added an answer on June 13, 2026 at 1:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to promote my app using app center but from the Web preview
I'm trying to develop image crop using JQuery. I use ajax to upload the
iam trying to show image preview before upload, for that i am using code
I am trying to create bitmap class using bitmapfactory. I get camera preview image
I am trying to make the same user interface to preview image like on
I am trying to generate 8 screenshots for an uploaded video using FFMPEG. I
I am practicing with PHP and AJAX but I have some problems! I'm trying
I'm trying to make a Preview function. I'm reading this blog, Django Admin Preview
Using the Windows 8 developer preview I'm trying to use a simple User Control
I'm trying to install the WCF REST Starter Kit Preview 2, but that fails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.