Skip to content

Instantly share code, notes, and snippets.

@pascalduez
Created December 24, 2011 14:01
Show Gist options
  • Save pascalduez/1517387 to your computer and use it in GitHub Desktop.
Save pascalduez/1517387 to your computer and use it in GitHub Desktop.
Drupal 7 — Basic Ajax request (Ajax framework)
<?php
/**
* @file
* Demo module, Basic Ajax request (Ajax framework).
*/
/**
* Implements hook_menu().
*/
function demo_menu() {
return array(
'demo/page' => array(
'page callback' => 'demo_page_callback',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
),
'demo/ajax' => array(
'page callback' => 'demo_ajax_callback',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
),
);
}
/**
* Demo page callback.
*/
function demo_page_callback() {
drupal_add_library('system', 'drupal.ajax');
$output = '<p>This is an ajax demo link: ';
$output .= l('Click me ! ', 'demo/ajax/nojs/', array('attributes' => array('class' => array('use-ajax'))));
$output .= '</p>';
return $output;
}
/**
* Demo Ajax callback.
*/
function demo_ajax_callback($type = 'ajax') {
if ($type == 'ajax') {
$output = 'Hello ! ';
$commands = array();
$commands[] = ajax_command_append('#block-system-main .content', $output);
$response = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($response);
}
else {
$output = 'Hello ! (no js) ';
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment