我正在尝试在提交后清理表格。我不确定我错过了什么。我将附上下面的PHP代码。这是我第一次使用 PHP 编写联系表单或任何东西,所以请多多包涵。提前谢谢你们!
抱歉,我以为我附上了代码。
<?php
if(isset($_POST['email'])) {
// EMAIL AND SUBJECT
$email_to = "##@##.com";
$email_subject = "New form submission";
function died($error) {
// your error code can go here
echo "<span style='color: red;' /><center> We're sorry, but there's errors found with the form you submitted.<br /><br /></center></span>";
echo $error."<br /><br />";
echo '<span style="color: red;" /><center>Please go back and fix these errors.<br /><br /></center></span>';
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.</center></span>');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$message = $_POST['message']; // 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 .= '<span style="color: red;" /><center>The Email Address you entered does not appear to be valid.<br /></center></span>';
}
$string_exp = "/^[0-9.-]+$/";
if(!preg_match($string_exp,$telephone) > 10) {
$error_message .= '<span style="color: red;" /><center>The Telephone number you entered does not appear to be valid.<br /></center></span>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= '<span style="color: red;" /><center>The First Name you entered does not appear to be valid.<br /></center></span>';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= '<span style="color: red;" /><center>The Last Name you entered does not appear to be valid.<br /></center></span>';
}
if(strlen($message) < 10) {
$error_message .= '<span style="color: red;" /><center>The Message you typed seems to be too short, add more words, please.</center></span><br />';
}
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 .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($message)."\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);
?>
<!-- The success html below -->
<span style="color: green;"><center>Thank you for contacting us. We will be in touch with you very soon.</center></span>
<?php
}
die();
?>