Drupal 7 中文教程 联系我们 两步表单 “联系我们”页面回调

<?php

/**
 * @file
 * 各种页面的回调函数.
 */
 
/**
 * “联系我们”页面的回调函数
 */
function contactus_page(){
  //我们为这个页面设置标题
 drupal_set_title('联系我们');
 
 $render_array = array(
   '#markup' => '',
 );
 
 //该页面的正文为一个表单,注意对于表单,这里需要使用drupal_render呈现一下。
 $render_array['#markup'] .= drupal_render(drupal_get_form('contactus_form'));
 
 //Drupal7的页面回调,返回的应该是一个数组
 return $render_array;
}

/**
 * “联系我们”表单的构建函数
 */
function contactus_form(){
  //添加我们自己的CSS,用来控制表单的样式
 drupal_add_css(drupal_get_path('module', 'contactus').'/contactus.css');
 
  //提示信息,默认为markup类型。
 $form['tips'] = array(
  '#prefix' =>'<div id="tips">',
  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
  '#suffix' =>'</div>',
 );
 
 //表单元素“姓名”
 $form['name'] = array(
   //表单元素的#title属性,对应于实际输出中的label
  '#title' => t('姓名'),
  //表单元素的类型,这里为textfield
  '#type' => 'textfield',
  //这个表单元素是必填的
  '#required' => TRUE,
  //表单元素的默认值,这里使用了三位运算符和isset进行判定
  '#default_value' =>isset($_SESSION['contactus_form']['name'])?$_SESSION['contactus_form']['name']:"",
  //表单元素的描述,
  '#description' => t('例如:周星驰'),
 );
 
  //表单元素“单位名称”
 $form['company_name'] = array(
  '#title' => t('单位名称'),
  '#type' => 'textfield',
  '#required' => TRUE,
  '#default_value' =>isset($_SESSION['contactus_form']['company_name'])?$_SESSION['contactus_form']['company_name']:"",
  '#description' => t('例如:北京无名信息技术公司'),
 );
 
  //表单元素“电子邮件”
 $form['mail'] = array(
  '#title' => t('电子邮件'),
  '#type' => 'textfield',
  '#required' => TRUE,
  '#default_value' =>isset($_SESSION['contactus_form']['mail'])?$_SESSION['contactus_form']['mail']:"",
  '#description' => t('例如:info@example.com'),
 );
 
 //表单元素“电话号码”
 $form['phone'] = array(
  '#title' => t('电话号码'),
  '#type' => 'textfield',
  '#required' => TRUE,
  '#default_value' =>isset($_SESSION['contactus_form']['phone'])?$_SESSION['contactus_form']['phone']:"",
  '#description' => t('例如:010-88888888'),
 );
 
  //表单元素“邮件正文”
 $form['contact'] = array(
  '#title' => t('邮件正文'),
  //表单元素的类型,这里为textarea
  '#type' => 'textarea',
  '#required' => TRUE,
  '#default_value' =>isset($_SESSION['contactus_form']['contact'])?$_SESSION['contactus_form']['contact']:"",
 );
 
 //访问来源的可选项
 $visit_options = array(
  'baidu' =>t('百度'),
  'google' =>t('谷歌'),
  'sohu' =>t('搜狐'),
  'other' =>t('其它'),
 );
 //表单元素“访问来源”
 $form['visit'] = array(
  '#title' => t('访问来源'),
  //表单元素的类型,这里为radios
  '#type' => 'radios',
  //单选按钮的可选项。
  '#options' => $visit_options,
  '#default_value' =>isset($_SESSION['contactus_form']['visit'])?$_SESSION['contactus_form']['visit']:"",
  //为了便于控制radios的外观,我们使用#prefix、#suffix为其添加了一个带有ID的div
  '#prefix' => '<div id="visit-radios">',
  '#suffix' => '</div>',
 );
 
 //表单元素“其它”,它依赖于表单元素“访问来源”
 $form['other'] = array(
  '#title' => t('其它'),
  '#type' => 'textfield',
  '#default_value' =>isset($_SESSION['contactus_form']['other'])?$_SESSION['contactus_form']['other']:"",
  //这里的意思是,当表单元素“访问来源”的值为“other”时,这个表单元素才显示出来
   '#states' => array(
    'visible' => array(
     ':input[name="visit"]' => array('value' => 'other'),
   ),
  ),
 );
 /*
 //表单元素“确认”提交按钮
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('确认'),
  );
  */
 
  //表单元素“确认”提交按钮,这里采用了图片的形式
  $form['image_submit'] = array(
   //表单元素的类型,这里为image_button
    '#type' => 'image_button',
  
  //图片按钮特有的#src属性,
    '#src' => drupal_get_path('module','contactus').'/images/button1-1.jpg',
    '#value' => 'image_sub',
  
  //为表单元素添加两个属性,onmouseout、onmouseover,为了在鼠标移到按钮上时,显示不同的图片效果
    '#attributes' =>array(
      'onmouseout' => "this.src='".base_path().drupal_get_path('module','contactus')."/images/button1-1.jpg'",
      'onmouseover' => "this.src='".base_path().drupal_get_path('module','contactus')."/images/button1-2.jpg'",
    ),
  
  //为了便于控制image_button的外观,我们使用#prefix、#suffix为其添加了一个带有ID的div
    '#prefix' => '<div id="image-submit">',
    '#suffix' => '</div>',
  );

  return $form;
}

/**
 * contactus_form表单的验证函数
 */
function contactus_form_validate($form, &$form_state){
  //这里使用正则表达式,来验证邮箱的有效性,注意,这里的正则表达式,包围在"/...../"之间。
 if(!preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $form_state['values']['mail'])){
  form_set_error('mail',t('您输入的电子邮件地址格式不正确'));
 }
}

/**
 * contactus_form表单的提交函数
 */
function contactus_form_submit($form, &$form_state){
  //把表单的值存放在会话中去,由于这里涉及到了两个不同的表单之间传值。
 $_SESSION['contactus_form'] = $form_state['values'];
 //表单重定向到确认页面
 $form_state['redirect'] = 'contactus/confirm';
}
 

Drupal版本: