这个drupal 6模块有什么问题?

这个drupal 6模块有什么问题?,drupal,drupal-6,drupal-modules,Drupal,Drupal 6,Drupal Modules,无论何时我点击submit按钮,表单都会被清除,里面的代码都不会被执行,不管我是在设置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 va

无论何时我点击submit按钮,表单都会被清除,里面的代码都不会被执行,不管我是在设置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');
?>

缺少一个}来关闭您的 功能模块测试提交($form,&$form\u state){

希望能有帮助


PR

您的代码有几个问题,但提交处理程序无法工作的原因只是因为您有错误的命名约定

让我们澄清所有这些:

您已将电子邮件字段设置为非必需,然后假设电子邮件地址字段在验证功能中可用。请更正为:

$form['email'] = array(
   '#title' => t('E-mail'),
   '#type' => 'textfield',
   '#size' => '25',   
   '#required' => TRUE,
   );
function module_test_validate($form, &$form_state) {
  $valid_email = $form_state['values']['email'];
    if(!valid_email_address($valid_email)) {
     form_set_error('email', t('The email address "@email" is invalid.', array('@email' => $valid_email)));
    }
}
我们现在允许使用标准验证函数来验证电子邮件地址

验证函数在清理用户输入时出现问题。请按以下方式更正:

$form['email'] = array(
   '#title' => t('E-mail'),
   '#type' => 'textfield',
   '#size' => '25',   
   '#required' => TRUE,
   );
function module_test_validate($form, &$form_state) {
  $valid_email = $form_state['values']['email'];
    if(!valid_email_address($valid_email)) {
     form_set_error('email', t('The email address "@email" is invalid.', array('@email' => $valid_email)));
    }
}
无法在打开的.module文件中返回任何内容。请删除以下内容:

/**
* Return the form.
*/
return drupal_get_form('module_test_form');
重命名验证函数,如下所示:

function module_test_form_validate($form, &$form_state) {
你的提交函数有几个问题。主要是它的名字

/**
* Create the form submit function
*/
function module_test_form_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: ' . check_plain($valid_name);
    $body[] = 'Email: ' . $valid_email;
    $body[] = 'Message: ' . nl2br(check_plain($valid_message));
    $body = implode('<br />', $body);
    $params = array(
    'body' => $body,
    'subject' => 'ModuleTest Form Confirmation ',
    );

    if($valid_check == 1){
        if (drupal_mail('module_test', 'some_mail_key', $valid_email, language_default(), $params, $from))
        {
            drupal_set_message(t('A copy has been sent to @email', array('@email' => $valid_email)));
        }
    }   
    if (drupal_mail('module_test', 'some_mail_key', $valid_email, language_default(), $params, $from))
    {
        drupal_set_message(t('An email has been sent to @email', array('@email' => $admin_email)));      
    } else {
        drupal_set_message('There was an error sending your email', 'error');
    }

}
/**
*创建表单提交功能
*/
功能模块测试表单提交($form,&$form\u state){
//主服务器详细信息
ini_集(“SMTP”、“mail.host”);
//smtp端口号
ini_集(“smtp_端口”,“25”);
//寄送地址
ini_集(“发送邮件自”user@domain.com");
$admin\u电子邮件地址: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'];
$fromuser@domain.com';
$body[]=“Name:”。选中($valid\u Name);
$body[]=“电子邮件:”。$valid_电子邮件;
$body[]=“Message:”.nl2br(check_plain($valid_Message));
$body=内爆('
',$body); $params=数组( “body”=>$body, “主题”=>“模块测试表单确认”, ); 如果($valid_check==1){ if(drupal_-mail('module_-test','some_-mail_-key',$valid_-email,language_-default(),$params,$from)) { drupal_set_消息(t('副本已发送到@email',数组('@email'=>$valid_email)); } } if(drupal_-mail('module_-test','some_-mail_-key',$valid_-email,language_-default(),$params,$from)) { drupal_set_消息(t('email已发送到@email',数组('email'=>$admin_email)); }否则{ drupal_set_消息(“发送电子邮件时出错”,“错误”); } }
好的,下面是可以复制粘贴的模块文件的完整代码。它可以工作(我测试过)。 尝试找出必要的更改。在可能/合适的情况下,沉迷于使用t()函数

<?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' => TRUE,
   );

   $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_form_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) {
  // these do not work in Drupal 7 //
  $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_form_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: ' . nl2br($valid_message);
    $body = implode('<br />', $body);
    $params = array(
    'body' => $body,
    'subject' => 'ModuleTest Form Confirmation ',
    );

    if($valid_check == 1){
        if (drupal_mail('module_test', 'some_mail_key', $valid_email, language_default(), $params, $from))
        {
            drupal_set_message(t('A copy has been sent to @email', array('@email' => $valid_email)));
        }
    }   
    if (drupal_mail('module_test', 'some_mail_key', $valid_email, language_default(), $params, $from))
    {
        drupal_set_message(t('An email has been sent to @email', array('@email' => $admin_email)));      
    } else {
        drupal_set_message('There was an error sending your email', 'error');
    }

}

提示:您可以使用php-l yourfile.module检查语法错误。在删除if语句和return之间的一些代码时,可能会出现我无意中删除了“}”的情况。