Search code examples
phpdrupal-7drupal-theming

Drupal 7 form not showing on page


I am new in Drupal. I have created a simple page and by using page node id I have created a page. I want to add form in that page but unable to do so but when I add any simple text, it's working but form elements not. I have no idea how to add form using form API.

<?php simple text which is showing on page ."; 
 $form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);

Solution

  • it can be done by module I have a .module file

    function module_name_menu() {
      $items = array();
      $items['module_name/form'] = array(
        'title' => t('My form'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('my_module_my_form'),
        'access arguments' => array('access content'),
        'description' => t('My form'),
        'type' => MENU_CALLBACK,
      );
      return $items;
    }
    function my_module_my_form($form, &$form_state) {
      $form['name']= array(
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#required' => TRUE,
        '#description' => "Please enter your name.",
        '#size' => 20,
        '#maxlength' => 20,
      );
       $form['mail'] = array(
        '#type' => 'textfield',
        '#title' => t('Email'),
        '#required' => TRUE,
        '#description' => "Please enter your Email.",
        '#size' => 30,
        '#maxlength' => 30,
      );
       $form['phno'] = array(
        '#type' => 'textfield',
        '#title' => t('Phone No'),
        '#required' => TRUE,
        '#description' => "Please enter your Contact Number.",
        '#size' => 30,
        '#maxlength' => 30,
      );  
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
      );
      return $form;
    }
    

    and then render this form by druple_get_form on any page where you want and this can be done by

    <?php
                $form = drupal_get_form('my_module_my_form');
                print drupal_render($form);
    
            ?>