所以我有一个死亡的功能,我让它回显一个带有错误信息的警报。问题是我关闭框后代码继续,它给出了我的成功消息。如果我添加exit;
died;
或died();
,则会将我重定向到白页...如果我将重定向标题重定向,则会在我看到警报框之前重定向。
这是功能和代码。
function died($error) {
echo '<script type="text/javascript">alert("We are very sorry, but there were error(s) found with the form you submitted. \nThese errors appear below.\n\n' . $error . '");</script>'; // your error code can go here
}
<?php
if (isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "admin@domain.com";
$email_subject = "NEW CLIENT!!!";
function died($error) {
echo '<script type="text/javascript">alert("We are very sorry, but there were error(s) found with the form you submitted. \nThese errors appear below.\n\n' . $error . '");</script>'; // your error code can go here
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.\n';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.\n';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.\n';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!---include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
答案 0 :(得分:0)
由于未设置$_POST['email']
,您的脚本不会返回任何内容。您的所有代码都在条件块内。
底部3行关闭原始if语句,如果设置了$_POST['email']
。这个文件应该在它自己的文件中提交表单。有一个单独的html文件,其中包含对此文件的操作。例如:<form action="thisfile.php">
答案 1 :(得分:0)
仅在未发现错误时发送邮件。正如成功的消息。因此在检查错误时是否需要额外的ELSE&gt;
下面添加0尝试将函数放在不在if语句
中的中心位置检查电子邮件地址的正则表达式是不正确的。例如foo@bar.travel将不匹配。
<?php
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
function died($error) {
echo '<script type="text/javascript">alert("We are very sorry, but there were error(s) found with the form you submitted. \nThese errors appear below.\n\n' . $error . '");</script>'; // your error code can go here
}
if (isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "admin@domain.com";
$email_subject = "NEW CLIENT!!!";
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$error_message = "";
// below regex used to be a bit valid years ago. not anymore
// $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!filter_input(INPUT_POST,'email', FILTER_VALIDATE_EMAIL)) {
$error_message .= 'The Email Address you entered does not appear to be valid.\n';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.\n';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.\n';
}
if(strlen($error_message) > 0) {
died($error_message);
}
else {
$email_message = "Form details below.\n\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!---include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
}
?>