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

Hi ,
I enabled this example module in modules . can u plz explain me where to run ..
Thank you

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..!!

Hi,

So we can't use for drupal 6?

iam using drupal 6 only , any solution for this?

Hi I have written this code in sites\all\modules\example\ folder and also active it from admin panel but how in my front end I can view it?
And is there any process to create a menu which show the the content of example module?

sir,
in my custom module after form submission there is blank entry in database...
can u plz suggest me how to solve this problem....

this is my form:----

function city_form() {
        $form['#attributes']['enctype'] = "multipart/form-data";
                $form['City Name']=array('#type'=>'textfield','#title'=>t('City Name'),'#size'=>t('30'));
                $form['submit']=array('#type'=>'submit','#value'=>'save');
                $form['#action'] = url('city');
                return $form;
}

this is my submit function:--
function city_form_submit($form, $form_states) {
        //$message="<pre>".print_r($form_states['values'],true)."</pre>";
         //$message="value inserted successfully";
         $val=$form_states['values'];
        $message=$val;
        if($val['submit']=='save')
         {
                //$validators=array();
       
               
         db_query("insert into {city} (city_name) values('".$val['City Name']."')");
         drupal_set_message(t($message));
         }
         else
         {
                drupal_set_message(t('there was a problem'));
         }
         
        }

thanx in advnc.......

Thank you so much. Your script is simply superb.

Sir,
I am new user of Drupal and I design a basic site from that. I just wanted to know how to make modules on my site written in PHP langauge.kindly suggest me the way to procceed further.
thanks a lot in advance.....

This article was very useful for me! thanks.
And I found a way to port it to drupal 6:

example.info:

; $Id$
name = Example
description = Example module
package = example module
core = 6.x
version = "6.x-1.0"

example.module:

<?php

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

function example_menu() {
  $items = array();

    $items['example'] = array(
      'title' => 'Example',
      'page callback' => 'drupal_get_form',
          'page arguments' => array('example_page_form'),
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK
    );
  return $items;
}

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));
}

thnx

I have done all this thing it working nicely. but when i submit button i used action = city it show me page not found error. where i can put city.php page. what should i do for it working.

Thanks

hey guys...i m very new to drupal.
i also getting same error of missing argument 1..pls can any1 help me out.

Thnx in advance

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.