2

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.");
  }
}

3 Answers 3

1

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.");
  }
}
0

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

5
  • div still not rendering. Dec 12, 2012 at 10:59
  • which div you trying to add that use-ajax class? l function adds that class to a tag Dec 12, 2012 at 11:02
  • yeah but after click the link it should print Hello World in <div id="sayinghello"></div>. Dec 12, 2012 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 Dec 12, 2012 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. Dec 12, 2012 at 11:29
0
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)
1
  • its showing the error: An AJAX HTTP error occurred. HTTP Result Code: 404 Debugging information follows. Path: /drupal7/customajaxcallback/ajax StatusText: Not Found Dec 12, 2012 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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