我制作了一個聯系表格,但是 PHP 只檢查“訊息”,如果填寫了所有其他檢查,則忽略。如果它沒有填寫所有其他檢查作業以及。有人可以幫我修復我的代碼嗎?謝謝。
<?php
$fullName = $customerEmail = $message = '';
$errors = array('fullName' => '', 'customerEmail' => '', 'message' => '');
if (isset($_POST['submit'])) {
$myEmail = "xxxxxxxxxxxxx";
$timeStamp = date("dd/M/YY HH:i:s");
$body = "";
$fullName = $_POST['fullName'];
$customerEmail = $_POST['customerEmail'];
$chosenSubject = $_POST['subject'];
$message = $_POST['message'];
$body .= "From: " . $fullName . " at $timeStamp" . "\r\n";
$body .= "Email: " . $customerEmail . "\r\n";
$body .= "Message: " . $message . "\r\n";
if (empty($fullName)) {
$errors['fullName'] = "Full name is required. <br>";
} else {
if (!preg_match("/^([a-zA-Z' ] )$/", $fullName)) {
$errors['fullName'] = "Your name must be a valid name.";
}
}
if (empty($customerEmail)) {
$errors['customerEmail'] = "An email is required. <br>";
} else {
if (!filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
$errors['customerEmail'] = "Email must be a valid email address.";
}
}
if (empty($message)) {
$errors['message'] = "A message is required.";
}
else {
mail($myEmail, $chosenSubject, $body);
header("Location: public/mail_sent.php");
}
}
?>
uj5u.com熱心網友回復:
最后一個 else 被更改為包含所有欄位的 IF 檢查,并且只有當它們都不為空時才會發送。
<?php
$fullName = $customerEmail = $message = '';
$errors = array('fullName' => '', 'customerEmail' => '', 'message' => '');
if (isset($_POST['submit'])) {
$myEmail = "[email protected]";
$timeStamp = date("dd/M/YY HH:i:s");
$body = "";
$fullName = $_POST['fullName'];
$customerEmail = $_POST['customerEmail'];
$chosenSubject = $_POST['subject'];
$message = $_POST['message'];
$body .= "From: " . $fullName . " at $timeStamp" . "\r\n";
$body .= "Email: " . $customerEmail . "\r\n";
$body .= "Message: " . $message . "\r\n";
if (empty($fullName)) {
$errors['fullName'] = "Full name is required. <br>";
} else {
if (!preg_match("/^([a-zA-Z' ] )$/", $fullName)) {
$errors['fullName'] = "Your name must be a valid name.";
}
}
if (empty($customerEmail)) {
$errors['customerEmail'] = "An email is required. <br>";
} else {
if (!filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
$errors['customerEmail'] = "Email must be a valid email address.";
}
}
if (empty($message)) {
$errors['message'] = "A message is required.";
}
if(!empty($fullName) && !empty($customerEmail) && !empty($message)) {
mail($myEmail, $chosenSubject, $body);
header("Location: public/mail_sent.php");
}
}
?>
uj5u.com熱心網友回復:
您的最后一個 else 應該更改為 IF,檢查是否有任何欄位不為空,然后發送訊息。
if(!empty($fullName) && !empty($customerEmail) && !empty($message)) {
mail($myEmail, $chosenSubject, $body);
header("Location: public/mail_sent.php");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311223.html
上一篇:PHP/HTML表格行在按下更新按鈕時不會更改為表格
下一篇:如果選中復選框,則添加URL
