How to create a contact form using FAPI in a custom module? I want to create a contact form using FAPI my custom module.

I created 2 files: one is .info and the other is .module. But I do not understand how I should link that module with my template name page--contact-us.tpl.php as I already activated that module from module list.

I used the code below in my module_name-module:

&lt;?php
  function my_module_form($form,&amp;$form_state) {
  $form['name'] = array(
    '#type' =&gt; 'textfield',
  );
  $form['company'] = array(
    '#type' =&gt; 'textfield',
  );
  $form['phone'] = array(
    '#type' =&gt; 'textfield',
  );
  $form['email'] = array(
    '#type' =&gt; 'textfield',
  );
  $form['message'] = array(
    '#type' =&gt; 'textfield',
  );
  return $form;
  }


And in my template.php file I wrote this code:

&lt;?php
 function mytheme_theme($existing, $type, $theme, $path) {
 $items['my_module_form'] = array(
 'render element' =&gt; 'form',
 'template' =&gt; 'page--contact-us',
 'path' =&gt; drupal_get_path('theme', 'mytheme') . '/template',
  );
  return $items;
  }
  ?&gt;


Finally I wrote a single line of code in my page--contact-us.tpl.php file to check whether a name field is appearing or not:

&lt;?php print render($form['name']); ?&gt;.

But nothing is happening on my contact-us page ... Why?

UPDATED: I also implemented hook_menu, here it is:

 function form_module_menu() {
 $items = array(); 
 $items['contact-us'] = array(
 'page callback' =&gt; 'drupal_get_form',
 'page arguments' =&gt; array('form_module_form'),
 'access callback' =&gt; TRUE
 );
 return 
 $items;
 }
When dealing with Form API in Drupal 7 there are two major steps:

Step 1: Create and implement the form in a module

In this step 1, you will setup the form, and then validate and submit the form, as further detailed below.

a) Setup the form

    function form_example() {
       $form['first_name']=array(
       '#type' =&gt; 'textfield',
       '#title' =&gt; t('First name'),
       '#description' =&gt; t('Enter first name'),
        );
        $form['last_name']=array(
        '#type' =&gt; 'textfield',
        '#title' =&gt; t('Last name'),
        '#description' =&gt; t('Enter last name'),
        );
    $form['submit_button'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; t('Click Here!'),
        );

    return $form;
    } 

    function form_example_validate($form, &amp;$form_state) {

    }
    function form_example_submit($form, &amp;$form_state) {

    }


b) Validate and submit the form

Now the form is ready to be displayed.

One way to display the form, is to theme the form in the module and then print the form.

function mytheme_theme($existing, $type, $theme, $path) {
   'theme_contact_form' =&gt; array (
        'variables' =&gt; array(
            'contact_form' =&gt; FALSE,
            ),
        'template' =&gt;'theme-contact-form',
    ),
 }
function mytheme_block_info() {
   $blocks['block_contact_form'] = array(
      'info' =&gt; t('My Theme Contact form'),
      );
   return $blocks;
}
function mytheme_block_view($delta = '') {
  $block = array();
  switch ($delta) {
  case 'block_contact_form':
        $block['content'] = array(
            '#theme' =&gt; 'theme_contact_form',
            '#contact_form' =&gt; drupal_get_form('form_example'),
            );
        break;
  }
  return $block;
}


Then create theme-contact-form.tpl.php in the module file and print the form:

&lt;?php print render($contact_form); ?&gt;


With all previous steps complpeted, the form is created and implemented in the module. 

Step 2: style the form

To style the form from theme layer, refer to How to a use a template file to theme a form?