[英]Send contact form data to email and show success/failure message on same page
電子郵件工作正常(電子郵件發送成功),但是當用戶單擊“提交”按鈕時,它僅重定向到contact.php文件。 我希望當用戶單擊按鈕時,它將在提交按鈕下方或上方的同一HTML文件中顯示成功消息,並且該消息也將被發送。 有人可以告訴我該怎么做嗎? 任何幫助和建議,將不勝感激。 謝謝
以下是HTML代碼:
<form action="contact.php" method="post">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Your Name *" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email *" required>
</div>
<div class="form-group">
<input type="text" name="phone" class="form-control" placeholder="Contact Number">
</div>
<div class="form-group">
<textarea class="form-control textarea" name="message" placeholder="Message"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="form-control btn-submit" Value="Send">
</div>
</form>
以下是我的PHP代碼:
<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from ='';
$email_subject = "Get In Touch";
$email_body = "User Name: $name \n".
"User Email: $email \n".
"Phone Number: $phone \n".
"User Message: \n
$message \n";
$to = "myemail@gmail.com";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
$result = mail($to,$email_subject,$email_body,$headers);
if($result) {
echo "Thank You for contacting us. Our team will get back to you
shortly.";
}
else{
echo "Something Went wrong. Please try again"; }
?>
注意:有兩個單獨的文件,一個用於HTML代碼為“ index.html”,另一個用於PHP代碼為“ contact.php”
提交表單時,您可以設置要提交的位置(在代碼中,您可以將表單提交給contact.php
:
<form action="contact.php" method="post">
如果您不希望ro提交到另一個文件,則可以使用php腳本在同一文件中編寫表單(例如,contact.php中的所有這些代碼 ),並且應從form的action屬性中刪除contact.php
:
<form action="" method="post">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Your Name *" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email *" required>
</div>
<div class="form-group">
<input type="text" name="phone" class="form-control" placeholder="Contact Number">
</div>
<div class="form-group">
<textarea class="form-control textarea" name="message" placeholder="Message"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="form-control btn-submit" Value="Send">
</div>
</form>
<?php
ob_start();
if (!empty($_POST)) {
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from ='';
$email_subject = "Get In Touch";
$email_body = "User Name: $name \n".
"User Email: $email \n".
"Phone Number: $phone \n".
"User Message: \n
$message \n";
$to = "myemail@gmail.com";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
$result = true;
if($result) {
echo "Thank You for contacting us. Our team will get back to you
shortly.";
} else {
echo "Something Went wrong. Please try again";
}
}
但這不是一個很好的選擇,其他選擇是使用AJAX
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.