Skip to content

Instantly share code, notes, and snippets.

@welly
Forked from pascalduez/demo.module
Last active August 29, 2015 14:18
Show Gist options
  • Save welly/673255c8a27bca8d8844 to your computer and use it in GitHub Desktop.
Save welly/673255c8a27bca8d8844 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Demo module, Basic Ajax form submit (Ajax framework).
*/
/**
* Implements hook_menu().
*/
function demo_menu() {
return array(
'demo/newsletter' => array(
'page callback' => 'drupal_get_form',
'page arguments' => array('demo_demo_form'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
),
);
}
/**
* A simple newsletter subscribe form.
*/
function demo_demo_form($form, &$form_state) {
return array(
'email' => array(
'#type' => 'textfield',
'#title' => t('Join our Newsletter'),
'#required' => TRUE,
'#attributes' => array(
'placeholder' => t('mail@example.com'),
),
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Subscribe'),
'#ajax' => array(
'callback' => 'demo_form_ajax_submit',
'wrapper' => 'demo-demo-form',
'method' => 'replace',
'effect' => 'fade',
),
),
);
}
/**
* Ajax callback function.
*/
function demo_form_ajax_submit($form, $form_state) {
// Dummy/dumb validation for demo purpose.
if (!empty($form_state['input']['email'])) {
return 'Subscribed !';
}
else {
return $form;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment