你好,所以這可能是一個愚蠢的問題,但我是一個初學者在PHP和想做一個電子郵件形式與驗證,當我點擊按鈕提交它重新加載頁面發送郵件即使不是所有的輸入都完成,並且點擊按鈕後,它表示沒有任何表單已經完成。我需要Ajax纔不會發生這種情況嗎? 這裏是你可能會更好地理解代碼: PHP的:Php電子郵件表單驗證,驗證不起作用
<?php
$nameErr = $emailErr = $messageErr = "";
$to = '[email protected]';
$subject = 'Email form test';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["name"])) {
$nameErr = "* Please enter your name";
} else {
$name = test_input($_POST["name"]);
}
if (isset($_POST["email"])) {
$emailErr = "* Please enter your email";
} else {
$email = test_input($_POST["email"]);
}
if (isset($_POST['message'])) {
$messageErr = "* Please enter a message";
} else {
$message = test_input($_POST["message"]);
}
$message = <<<EMAIL
$name
$email
$message
EMAIL;
$header = '$email';
if ($_POST['submit']){
mail($to , $subject , $message , $header);
$feedback = 'Your email has been sent.';
}
}
?>
HTML:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="name">
<input type="text" name="name" id="name" value="" placeholder="Name" size="50">
<span class="error"><?php echo $nameErr;?></span>
</div>
<br>
<div class="email">
<input type="text" name="email" id="email" value="" placeholder="E-Mail" size="50">
<span class="error"><?php echo $emailErr;?></span>
</div>
<br>
<div class="message">
<textarea name="message" id="message" placeholder="Enter your message" rows="6"></textarea>
<span class="error"><?php echo $messageErr;?></span>
</div>
<br>
<div class="submit">
<input type="submit" name="submit" class="button special">
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
謝謝。
「*無論窗體處於什麼狀態,我都會調用mail() - 爲什麼它發送電子郵件,無論窗體處於什麼狀態?*」。不,你不需要AJAX,只是當你不想發送郵件時不要調用發送郵件功能。 – TessellatingHeckler