2013-04-23 73 views
0

我正在構建PHP註冊系統。如果有新用戶註冊到我的網站,它會發送一封通知郵件給我。但問題是,如果用戶不輸入任何內容,它也會向我發送電子郵件。我如何解決這個問題?在發送通知郵件之前對php註冊進行驗證

這是驗證部分。

/*Validation Begins*/ 
if(empty($_POST) === false) { 
     $required_fields = array('school_name', 'mailing_address', 'postcode', 'courier_address', 'courier_postcode', 'courier_postcode', 'phonenumber', 'faxnumber', 'email', 'website', 'principal_fullname', 'principal_phonenumber', 'principal_email'); 
     foreach($_POST as $key=>$value){ 
      if(empty($value) && in_array($key, $required_fields) === true){ 
       mysql_close(); 
       ?> 
       <script type="text/javascript"> 
       alert("Fields marked with an asterisk are required"); 
       history.back(); 
       </script> 
       <?php 
      } 
    } 

下面是插入函數和郵件函數。

function register_school($register_data){ 
$fields = '`' . implode('`, `', array_keys($register_data)) . '`'; 
$data = '\'' . implode('\', \'', $register_data) . '\''; 

mysql_query("INSERT INTO `schools_info` ($fields) VALUES ($data)"); 

    ?> 
    <script type='text/javascript'> 
    alert("Registration Successful!"); window.location.href = '/registration-success/'; 
    </script> 
    <?php 
    $schoolname = $_POST['school_name']; 
    $mailing_address = $_POST['mailing_address']; 
    $postcode = $_POST['postcode']; 
    $courier_address = $_POST['courier_address']; 
    $phonenumber = $_POST['courier_postcode']; 
    $faxnumber = $_POST['faxnumber']; 
    $email = $_POST['email']; 
    $website = $_POST['website']; 
    $principal_fullname = $_POST['principal_fullname']; 
    $principal_phonenumber = $_POST['principal_phonenumber']; 
    $principal_email = $_POST['principal_email']; 

    $to = "[email protected]"; 
    $subject = "New Registered School"; 
    $message = "School Name: $schoolname\r\nSchool Address: $mailing_address\r\nPostcode: $postcode\r\nCourier Address: $courier_address\r\nCourier Postcode: $courier_postcode\r\nPhone Number: $phonenumber\r\nFax Number: $faxnumber\r\nEmail: $email\r\nWebsite: $website\r\nPrincipal Name: $principal_fullname\r\nPrincipal Phone Number: $principal_phonenumber\r\nPrincipal Email: $principal_email"; 
    $from = "testing.com"; 
    $headers = "From:" . $from; 
    mail($to,$subject,$message,$headers); 

} 

註冊學校

if(empty($_POST) === false){ 
$register_data = array(
    'school_name'   => $_POST['school_name'], 
    'mailing_address'  => $_POST['mailing_address'], 
    'postcode'    => $_POST['postcode'], 
    'courier_address'  => $_POST['courier_address'], 
    'courier_postcode'  => $_POST['courier_postcode'], 
    'phonenumber'   => $_POST['phonenumber'], 
    'faxnumber'    => $_POST['faxnumber'], 
    'email'     => $_POST['email'], 
    'website'    => $_POST['website'], 
    'principal_fullname' => $_POST['principal_fullname'], 
    'principal_phonenumber' => $_POST['principal_phonenumber'], 
    'principal_email'  => $_POST['principal_email'], 
    'CScoor'    => $_POST['CScoor'], 
    'CS_email'    => $_POST['CS_email'], 
    'CS_phone'    => $_POST['CS_phone'], 
    'Engcoor'    => $_POST['Engcoor'], 
    'Eng_email'    => $_POST['Eng_email'], 
    'Eng_phone'    => $_POST['Eng_phone'], 
    'Mcoor'     => $_POST['Mcoor'], 
    'M_email'    => $_POST['M_email'], 
    'M_phone'    => $_POST['M_phone'], 
    'Sccoor'    => $_POST['Sccoor'], 
    'Sc_email'    => $_POST['Sc_email'], 
    'Sc_phone'    => $_POST['Sc_phone'] 

); 
register_school($register_data); 

mysql_close(); 

}

+1

月1日,則不能使用1個功能,使數據庫INSERT,輸出JavaScript和發送EMIAL ... 這是如此瘋狂和錯誤.. +你有$ register_data然後你使用$ _POST你的電子郵件..真的你需要閱讀有關如何清理你的輸入... – Svetoslav 2013-04-23 07:04:37

+0

你在哪裏調用'register_school()'? – Amir 2013-04-23 07:23:24

+0

@Amir我已經更新了我的問題,請看看。 – rapide 2013-04-23 07:27:06

回答

0

部分之前:

$to = "[email protected]"; 
    $subject = "New Registered School"; 
    $message = "School Name: $schoolname\r\nSchool Address: $mailing_address\r\nPostcode: $postcode\r\nCourier Address: $courier_address\r\nCourier Postcode: $courier_postcode\r\nPhone Number: $phonenumber\r\nFax Number: $faxnumber\r\nEmail: $email\r\nWebsite: $website\r\nPrincipal Name: $principal_fullname\r\nPrincipal Phone Number: $principal_phonenumber\r\nPrincipal Email: $principal_email"; 
    $from = "testing.com"; 
    $headers = "From:" . $from; 
    mail($to,$subject,$message,$headers); 

開始。檢查每個變量是否爲空:

//編輯:對不起,沒有看到你有一個關於值的數組。然後,你可以把它像這樣:所有的

$success = true; 
foreach($register_data as $data) { 
    if (empty($_POST[$data])) { 
     $success = false; 
     break; 
    } 
} 

if ($success == true) { 
    //then mail 
} 
+0

@rapide這段代碼真的很醜。在函數中拆分代碼的不同部分。這會更好地閱讀和調試。 – Zaziki 2013-04-23 07:43:00

0
if(empty($_POST) === false) { 
      $required_fields = array('school_name', 'mailing_address', 'postcode', 'courier_address', 'courier_postcode', 'courier_postcode', 'phonenumber', 'faxnumber', 'email', 'website', 'principal_fullname', 'principal_phonenumber', 'principal_email'); 
      $flag = TRUE; 
      foreach($_POST as $key=>$value){ 
       if(empty($value) && in_array($key, $required_fields) === true){ 
        mysql_close(); 
        $flag = FALSE; 
        ?> 
        <script type="text/javascript"> 
        alert("Fields marked with an asterisk are required"); 
        history.back(); 
        </script> 
        <?php 
       } // end second if 
     } //end foreach 
     if ($flag == TRUE){ 
     $register_data = array(.....); //all $_POST value assign here 
     register_school($register_data);} 
} //end first if 
相關問題