Drupal Forms API Basics

Drupal Forms API is used to create Drupal-compatible form elements with PHP, mostly from within modules. Saying "Drupal-compatible" we mean, that the code produced will be AHAH/AJAX compatible and will be marked-up in a way that is expected from of Drupal, thus providing code and theming consistency.

Forms are created in functions, each form element being an array. Then, in the right place, the function gets called as an argument of the drupal_get_form function.

<?php
 
echo drupal_get_form('my_form');
?>

Code for the form 'my_form' can be like this:

<?php
function my_form() {

$form=array();

 
$form['intro'] = array(
   
'#type' => 'markup',
   
'#value' => '<p>This is a HTML part of the form used to give suggestions on how to fill it. The element&apo;s type is 'markup'.</p>',
  );

 
$form['name'] = array(
   
'#type' => 'textfield',
   
'#title' => 'Please provide your name',
   
'#description' => 'Please type in your name in capital letters, no more than 1024 letters totally.',
  );

 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => 'Submit',
  );

  return
$form;

}
?>