How to make a simple form module in Drupal

This example module will provide a form where you can enter your name and upon submission will display your name as a message on the following page. It is very basic but will give you a start to making your first module.

You can change all instances of the word example to the name of your module.

First you need to create a file called example.info:

name = Example
description = "Example module."
version = "5.x-1.0"
project = "example"

Next you need the example.module file:

<?php

function example_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('This module implements an example form.');
  }
}

function example_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'example',
      'title' => t('Example'),
      'callback' => 'example_page',
      'access' => TRUE,
      'type' => MENU_CALLBACK
    );
  }
  return $items;
}

function example_page() {
  return drupal_get_form('example_page_form');
}

function example_page_form() {
  $form['fullname'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your full name'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

function example_page_form_submit($form_id, $form_values) {
  $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_values,true) . '</pre>';
  drupal_set_message(t($message));
}

?>

Now you can enable the module, and visit:

http://yoursite.com/example

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Once i have created basic module then store in sites/all/module folder.

Then how will I create it to show output online web browser, once module is enabled.

Waiting for your reply, i will appreciate it!

Stephanos Sam

brett's picture

goto your module management and enable the module, then visit yoursite.com/example

just go to admin part and enabled it from there and then go to blocks and select the location where you want to display you modules contents

When can i buy you a beer?

brett's picture

haha, i just wrote it down so i could look it up later. if it helps others too then thats even better! =)

plese tell me,

how to submit a form in database ,and validation also
i m waiting.

thanks

This is amazing work. Do you happen to have it for Drupal 6? Would love to see that...

really a good exemple thanx

hi this is not working for me this showing a
Missing argument 1 for example_menu() in C:\wamp\www\drupal-6.15\sites\all\modules\example\example.module on line 9 error

Nice tutorial. Very helpful.

How can i add to a form buttom events? Like on_mouse_click="my_js_func()"

Hi Guys

I am also getting the :- warning: Missing argument 1 for example_menu() in /home/web/intranet_andy/sites/all/modules/example/example.module on line 10.

Is this because I am using Drupal 6.x

Any help greatly appreciated

Yup.. u hv got it right buddy..

warning: Missing argument 1 for example_menu() in /home/web/intranet_andy/sites/all/modules/example/example.module on line 10.

--u r geting this warning just because of Drupal 6.x

Do one thing, download & install a previous version of Drupal and then try to make a module by using this code..

U'll get a Bingo..!!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <i> <strong> <cite> <em> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <css>, <diff>, <drupal5>, <html>, <javascript>, <php>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.