Created
September 9, 2014 14:50
-
-
Save anonymous/f6d8dc19a3eed0f56d59 to your computer and use it in GitHub Desktop.
drupal form example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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