How to Create form in PHP based Drupal as a module?

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


<?php
function appointment_menu($may_cache) {
   
$items = array();
   
$items[] = array(
   
'path' => 'app',
   
'title' => t('App'),
   
'callback' => 'drupal_get_form'// tell drupal to display a form
   
'callback arguments' => array('appointment_form'), // arguments for the callback have to be in an array. The form name is now 'appointment_form'
   
'access' => user_access('access content'),
    
'type' => MENU_CALLBACK);
    return
$items;
}

// Builder function needs the same name as form
function appointment_form() {
   
$form['name'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Name'),
   
'#size' => 30,
   
'#maxlength' => 64);
  ...
 
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
 
$output = drupal_get_form('appointment_form', $form);
  return
$output;
}

// Validate function is form name with 'validate' suffix
function appointment_form_validate($form_id, $form_values) {
  
form_set_error('', t('Error messages show here.'));
}

// Submit function is form name with 'submit' suffix
function appointment_form_submit($form_id, $form_values) {
   
drupal_set_message(t('Your form has been saved.'));
}
?>

                                     

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.