0

I am trying to upload an image in my template page, using jquery ajax-form submit.

And i am returning just the image name after it is uploaded in the module function. However, i am getting the entire html as response.

In my theme's template.php, i have used the following code:

function my_theme_preprocess_page(&$vars) { 
  if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
        $vars['template_file'] = 'page-ajax';
  }
}

I have this code in my theme's page-ajax.tpl.php: <?php print $content; ?>

And i am passing the 'ajax=1' parameter to the module hook menu function that handles the image upload. How can i just output the image name in ajax response, instead of the entire html.tpl.php?

1

How are you generating the ajax response?

If you simply want to return a string eg a file name you can just return it as plain text or as JSON using hook_menu and a callback function eg:

function mymodule_menu() {
    $items['my/custom/path'] = array(
      'page callback' => 'mymodule_callback',
    );
    return $items;
}
function mymodule_callback() {
  //Do stuff like get filename from DB.
  print 'filename.jpg';

  //to return JSON use drupal_json
  //drupal_json(array('filename' => 'filename.jpg'))
}
  • Sorry for being late. In my module function, i am returning just the string output, and then using exit. For example, <?php echo $string; exit(); ?> After clearing the cache, everything is working well now. – shasi kanth May 6 '13 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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