Community Documentation

2. Basic Form with submit button

Last updated November 30, 2012. Created by bekasu on February 18, 2010.
Edited by ifernando. Log in to edit this page.

Code sample #2:

This form adds a "Submit" button--Notice what happens on submission.

<?php

function my_module_menu() {
 
$items = array();
 
$items['my_module/form'] = array(
   
'title' => t('My form'),
   
'page callback' => 'my_module_form',
   
'access arguments' => array('access content'),
   
'description' => t('My form'),
   
'type' => MENU_CALLBACK,
  );
  return
$items;
}

function
my_module_form() {
  return
drupal_get_form('my_module_my_form');
}

function
my_module_my_form($form_state) {
 
$form['name'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Name'),
  );
 
 
// Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.
 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => 'Submit',
  );
  return
$form;
}
?>

About this page

Drupal version
Drupal 6.x, Drupal 7.x, Drupal 8.x
Level
Beginner

Develop for Drupal

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here