0

我正在构建一个 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 = "example@hotmail.com";
    $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();

}

4

2 回答 2

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
于 2013-04-23T08:01:40.497 回答
0

部分前:

    $to = "example@hotmail.com";
    $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
}
于 2013-04-23T07:37:12.727 回答