简体   繁体   中英

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!'),
);

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

        ?>

This should help to point you in the right direction. Drupal 7 - How to Make a Simple Module with a Form and Menu Link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM