function confirm()
は、コンタクトフォームを正常に送信したときにユーザーにポップアップ確認を送信する必要があります。うまくいくようですが、ユーザーがブラウザでページの更新を押すと、その機能が再度トリガーされ、リンクされたページからページに戻ると、confirm()
関数が再度トリガーされます。function confirm()は、ページリフレッシュでない有効なフォーム提出後にのみトリガーする必要があります。
なぜ私は、confirm()
関数の前に関数が呼び出されるのを防ぐべきである2つの変数をリセットするため、理解できません。
<?PHP
/* Set e-mail recipient */
$myemail = "[email protected]";
/* Introduce the email message */
$themessage = "";
$nameErr = $emailErr = $subjectErr = $commentErr = "";
$name = $email = $subject = $comment = "";
$nosubmit = 0; //variable to check whether form data valid
$nosubmit_two = 0; //variable to check whether form data valid
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function confirm() {
echo "<script>";
echo "window.confirm('Thank you for your message. You should get an
email confirmation. I will reply to you in full as soon as I can. Have
a nice day!')";
echo "</script>";
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["Name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "* Only letters and white space allowed";
}
else $nosubmit = 1;
$email = test_input($_POST["Email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "* Invalid email format";
}
else $nosubmit_two = 1;
$subject = test_input($_POST["Subject"]);
$comment = test_input($_POST["Comment"]);
if ($nosubmit == "1" && $nosubmit_two == "1") {
$themessage = "Message From:".$name."on the matter
of:".$subject."Message reads:".$comment;
mail($myemail, $subject, $themessage);
$name = $email = $subject = $comment = "";
$nosubmit = 0;
$nosubmit_two = 0;
confirm(); // write function to alert user that email has been sent;
}
} // closes main if statement
?>
ありがとうございました。私はそれを試してみましょう:) – Ginger