Posted by droweski on April 30, 2012 at 11:45pm
Anybody see why this code fails.
Should be simple ajax div replace, but not working.
Thanks,
<?php
/**
* Implements hook_menu().
*/
function ajax_page_menu() {
$items = array();
$items['ajax_page_link'] = array(
'title' => 'Ajax page-replace demo',
'page callback' => 'ajax_page_link',
'access callback' => TRUE,
);
// A menu callback for using drupal ajax outside of the Form API.
$items['ajax_page_link_callback'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'ajax_page_link_replace',
'access callback' => TRUE,
);
return $items;
}
// Link callback.
function ajax_page_link() {
drupal_add_js('misc/ajax.js');
drupal_add_js('misc/drupal.js');
$link = l(t('Click here for action'), 'ajax_page_link_callback/nojs/', array('attributes' => array('class' => array('use-ajax'))));
return '<div>' . $link . '</div><div id="target">This is the target div with initial content</div>';
}
// Replace action callback.
function ajax_page_link_replace($type = 'ajax') {
$output = t("This is the replacement text");
if ($type == 'ajax') {
$commands = array();
$commands[] = ajax_command_replace('#target', '$output');
$page = array(
'#type' => 'ajax_commands',
'#ajax_commands' => $commands
); ;
ajax_deliver($page_callback_result);
}
else {
$output = t("default pagelink, ajax failed");
return $output;
}
}