Ajax Link (ajax without form)

function modulename_menu() {
  $items['modulename_ajax'] = array(
    'page callback' => 'modulename_searchfunction_ajax',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}
function modulename_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'book') {
    $body = field_get_items ('node', $node, 'body');
    $code = $body[0]['value'];
    $pattern = '/function[\s\n]+(\S+)[\s\n]*\(/';
 
     if (preg_match_all ($pattern, $code, $matches)) {
       $function_name = $matches[1];
     }
  }
 
  if (!empty($function_name)) {
    foreach($function_name as $fun_key => $fun_value) {
      $link = array(
        '#type' => 'link',
        '#title' => $fun_value,
        '#href' => 'modulename_ajax/nojs/' . $fun_value,
        '#suffix' => '<div id="ak"></div>',
        '#ajax' => array(),
      );
      $node->content['function_name'][] = $link;
    }
  }
}
 
function modulename_searchfunction_ajax($type = 'ajax', $term = '') {
  if ($type == 'ajax') {
    $view = views_get_view('search');
    $view->set_display ('block_1');
    $view->set_arguments(array(check_plain($term)));
    $output = $view->render();
    $commands = array();
    $commands[] = ajax_command_replace ('#ak', $output);
    $page = array (
      '#type' => 'ajax',
      '#commands' => $commands,
    );
    ajax_deliver($page);
  }
  else {
    $output = t("This is some content delivered via a page load.");
    return $output;
  }
}
Help Share this Article