Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I added a class use-ajax to a link but Its not updating the wrapper `div. My Code is:

function custom_menu(){
  $items['testing'] = array(
    'page callback' => 'articlenodeform',
    'access arguments' => array('access content'),
  );
  $items['customajaxcallback'] = array(
   'page callback' => 'custom_ajaxcallback',
   'access arguments' => array('access content'),
  );
  return $items;
}

function articlenodeform(){
   drupal_add_library('system', 'drupal.ajax'); 
   drupal_add_js('misc/ajax.js');
   $link = l(t('Say Hello'), 'customajaxcallback/ajax', array('attributes' => array('class' => 'use-ajax')));
   return '<div>'.$link.'</div><div id="sayinghello"></div>';
}

function custom_ajaxcallback($type = 'ajax') {
  if($type == 'ajax') {
    $output = t('Hello World!');
    $command = array();
    $commands[] = ajax_command_append('#sayinghello', $output);
    $page = array('#type' => 'ajax', '#ajax_command' => $commands);
    ajax_deliver($page);
  }
  else{
    return t("Hello world in new page.");
  }
}
share|improve this question
add comment

3 Answers 3

Change your link to this:

$link = l(t('Say Hello'), 'customajaxcallback/ajax', array('attributes' => array('class' => array('use-ajax'))));

class element itself must also be an array. read about it here

share|improve this answer
    
div still not rendering. –  Sumit Madan Dec 12 '12 at 10:59
    
which div you trying to add that use-ajax class? l function adds that class to a tag –  neok Dec 12 '12 at 11:02
    
yeah but after click the link it should print Hello World in <div id="sayinghello"></div>. –  Sumit Madan Dec 12 '12 at 11:06
    
try to set access callback to true(Or try the permission better fitting your needs) in custom_menu and also set 'type' => MENU_CALLBACK –  neok Dec 12 '12 at 11:25
    
I am logged in as admin so no need for access callback and i added the 'type' => MENU_CALLBACK but still not working. –  Sumit Madan Dec 12 '12 at 11:29
add comment
function custom_menu(){
  $items['testing'] = array(
    'page callback' => 'articlenodeform',
    'access arguments' => array('access content'),
  );
  $items['customajaxcallback/%'] = array(
   'page callback' => 'custom_ajaxcallback',
   'access arguments' => array('access content'),
    'page arguments' => array(1),
  );
  return $items;
}

function articlenodeform(){
   drupal_add_library('system', 'drupal.ajax'); 
   $link = l(t('Say Hello'), 'customajaxcallback/nojs', array('attributes' => array('class' => 'use-ajax')));
   return '<div>'.$link.'</div><div id="sayinghello"></div>';
}

function custom_ajaxcallback($ajax) {
  if($ajax == 'ajax') {
    $output = t('Hello World!');
    $command = array();
    $commands[] = ajax_command_append('#sayinghello', $output);
    $page = array('#type' => 'ajax', '#ajax_command' => $commands);
    ajax_deliver($page);
  }
  else{
    return t("Hello world in new page.");
  }
}

edit: If you noticed, there were several changes made in the code.

  1. link has been changed to customajaxcallback/nojs
  2. hook menu is now customajaxcallback/%
  3. We pass argument 1 to the callback function custom_ajaxcallback 'page arguments' => array(1),
  4. Callback function has been changed by removing the default value and making it look like this custom_ajaxcallback($ajax)
share|improve this answer
    
its showing the error: An AJAX HTTP error occurred. HTTP Result Code: 404 Debugging information follows. Path: /drupal7/customajaxcallback/ajax StatusText: Not Found –  Sumit Madan Dec 12 '12 at 12:05
add comment
up vote 0 down vote accepted

I did it : The problem was in my callback function :

function custom_ajaxcallback($type = 'ajax') {
  if($type == 'ajax') {
    $output = t('Hello World!');
    $command = array();
    $commands[] = ajax_command_append('#sayinghello', $output);
    $page = array('#type' => 'ajax', '#commands' => $commands); --> In this line
    ajax_deliver($page);
  }
  else{
    return t("Hello world in new page.");
  }
}
share|improve this answer
add comment

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.