2012-11-20 262 views
0

每当我点击提交按钮时,表单被清除并且没有执行任何代码,尽管我正在设置drupal消息或尝试发送电子邮件。我用'[email protected]'取代了我的电子邮件等。我在做这个drupal 6模块有什么问题?

<?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","[email protected]"); 

    $admin_email = '[email protected]'; 
    $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 = '[email protected]'; 
    $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'); 
?> 
+0

提示:您可以使用PHP -l yourfile.module检查语法错误 – m4t1t0

回答

0

缺少}关闭您的 功能module_test_submit($形式,& $ form_state){

希望它有助于

PR

+0

这样看来,我不小心删除了“}” if语句和收益之间在删除一些代码时。 – damm

+0

模块正在工作吗? –

+0

nope,它不发送任何电子邮件 – damm

0

你的代码中有几个问题,但原因您的提交处理程序不起作用仅仅是因为您命名惯例错误。

让我们澄清了所有这些:

您已设置为不需要的电子邮件字段,然后假设电子邮件地址字段是在验证功能可用。将其修正为:

$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","[email protected]"); 

    $admin_email = '[email protected]'; 
    $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 = '[email protected]'; 
    $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'); 
    } 

} 

好的,下面是您可以复制粘贴的模块文件的完整代码。它工作(我测试)。 试图弄清楚什么是必要的改变。尽可能/合适地使用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","[email protected]"); 

    $admin_email = '[email protected]'; 
    $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 = '[email protected]'; 
    $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'); 
    } 

}