我正在构建一个 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();
}