2012-11-20 3 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

아니요, 해당 이메일을 보내지 않음 – 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'); 
    } 

} 
관련 문제