Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer

up vote 1 down vote accepted

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'))
}
share|improve this answer
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. – dskanth May 6 at 3:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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