Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created September 9, 2014 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f6d8dc19a3eed0f56d59 to your computer and use it in GitHub Desktop.
Save anonymous/f6d8dc19a3eed0f56d59 to your computer and use it in GitHub Desktop.
drupal form example
<?php
/**
* Implements hook_permission()
*/
function ghost_form_permission() {
return array(
'submit ghost_form' => array(
'title' => t('Submit ghost_form'),
'description' => t('Submit the ghost_form form'),
),
);
}
/**
* Implements hook_menu()
*/
function ghost_form_menu() {
$items = array();
$items['ghost-form-example'] = array(
'title' => 'Ghost Example Form',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('submit ghost_form'),
'page callback' => 'drupal_get_form',
'page arguments' => array('ghost_form_form'),
);
return $items;
}
/**
* My Example Form
*/
function ghost_form_form($form, &$form_state) {
$form['field_first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#size' => 255,
'#description' => t('Please enter your first name.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add item'),
);
return $form;
}
/**
* Validation handler for the ghost_form_form
*/
/* function ghost_form_form_validate($form, &$form_state) {
//print('<pre>'.print_r($form_state['values'],1).'</pre>');die();
if (!is_numeric($form_state['values']['field_first_name'])) {
form_set_error('field_first_name', t('You must enter a valid number'));
return FALSE;
}
return TRUE;
}*/
/**
* Submit handler for the ghost_form_form. Inserts entry into the database
*/
function ghost_form_form_submit($form, &$form_state) {
$form = db_insert('field_data_field_first_name') // database table name
->fields(array(
'field_first_name_value' => $form_state['values']['field_first_name_value'],
))
->execute();
drupal_set_message(t('Your form entry has been added'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment