我正在构建一个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();
}
答案 0 :(得分: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
}
答案 1 :(得分: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