编程语言
首页 > 编程语言> > php-Drupal 7表单未显示在页面上

php-Drupal 7表单未显示在页面上

作者:互联网

我是Drupal的新手.我创建了一个简单页面,并使用页面节点ID创建了一个页面.我想在该页面中添加表单,但是无法这样做,但是当我添加任何简单的文本时,它就可以了,但是表单元素却没有.我不知道如何使用表单API添加表单.

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

解决方法:

可以通过模块来完成,我有一个.module文件

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

然后通过druple_get_form在所需的任何页面上呈现此表单,可以通过

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

        ?>

标签:drupal-theming,drupal-7,php
来源: https://codeday.me/bug/20191118/2028055.html