每当我单击提交按钮时,表单都会被清除,并且不会执行任何内部代码,尽管我正在设置 drupal 消息或尝试发送电子邮件。我已经用“user@domain.com”替换了我的电子邮件等。
<?php
/**
* implementation of help hook
*/
function module_test_help($path, $arg) {
switch ($path) { // if the path is equaled to case, return value.
case 'admin/help#module_test':
$output .= '<p>' . t('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis dictum turpis.') . '</p>';
return $output;
break;
}
}
/**
* implementation of menu hook
*/
/** function module_test_menu() {
* $items = array();
* $items['lorem-ipsum'] = array (
* 'title' => t('Lorem-ipsum'),
* 'page callback' => '_module_test_page',
* 'access arguments' => array('administer my module'),
* 'type' => MENU_CALLBACK,
* );
* return $items;
* }
**/
function module_test_menu() {
$items = array();
$items['validation_form'] = array (
'title' => t('Validation Form'),
'page callback' => 'drupal_get_form',
'page arguments' => array('module_test_form'),
'access arguments' => array('administer my module'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* implementing permissions
**/
function module_test_perm() {
return array('Administer my module');
}
/**
* implementing forms with validations
**/
function module_test_form() {
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#size' => '25',
'#required' => FALSE,
);
$form['email'] = array(
'#title' => t('E-mail'),
'#type' => 'textfield',
'#size' => '25',
'#required' => FALSE,
'#element_validate' => array('module_test_validate'),
);
$form['message'] = array(
'#title' => t('Message'),
'#type' => 'textarea',
'#required' => FALSE,
);
$form['checkbox'] = array(
'#title' => t('Send yourself a copy'),
'#type' => 'checkbox',
'#required' => FALSE,
'#return_value' => 1,
'#default_value' => 0
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return $form;//
}
/**
* Validation of e-mail form
**/
function module_test_validate($form, &$form_state) {
$valid_email = $form_state['values']['email'];
if(!valid_email_address($valid_email)) {
form_set_error('email', 'The email address "' . $valid_email . '" is invalid.');
}
}
/**
* implementing mail function
*/
function module_test_mail($key, &$message, $params) {
$headers = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
foreach ($headers as $key => $value) {
$message['headers'][$key] = $value;
}
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
}
/**
* Create the form submit function
*/
function module_test_submit($form, &$form_state) {
//main server details
ini_set("SMTP", "mail.host");
//smtp port number
ini_set("smtp_port", "25");
//send from address
ini_set("sendmail_from","user@domain.com");
$admin_email = 'user@domain.com';
$valid_name = $form_state['values']['name'];
$valid_email = $form_state['values']['email'];
$valid_message = $form_state['values']['message'];
$valid_check = $form_state['values']['checkbox'];
$from = 'user@domain.com';
$body = 'Name: ' . $valid_name;
$body = 'Email: ' . $valid_email;
$body = 'Message: ' . $valid_message;
$params = array(
'body' => $body,
'subject' => 'ModuleTest Form Confirmation ',
);
if($valid_check == 1){
if (drupal_mail('module_test_form', 'some_mail_key', $valid_email, language_default(), $params, $from, $send = TRUE))
{
form_set_error('checkbox', 'A copy has been sent to ' .$valid_email .'');
}
}
if (drupal_mail('module_test_form', 'some_mail_key', $valid_email, language_default(), $params, $from, $send = TRUE))
{
drupal_set_message('An email has been sent to ' . $admin_email);
} else {
drupal_set_message('There was an error sending your email');
}
}
/**
* Return the form.
*/
return drupal_get_form('module_test_form');
?>