2014-09-02 109 views
0

我想創建一個窗體,以顯示在WordPress的網站側欄。作爲基本步驟,我創建了沒有驗證的表單。代碼如下。如何在發送電子郵件後顯示成功消息?

<?php 

if($_POST["submit"]) { 
    $recipient="[email protected]"; 
    $subject="New reservations request"; 
    $sender=$_POST["sendername"]; 
    $senderEmail=$_POST["senderEmail"]; 
    $message=$_POST["message"]; 
    $name_title=$_POST["name_title"]; 

    $mailBody="Name: $name_title $sender\nEmail: $senderEmail\n\n$message"; 

    mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>"); 

    $thankYou="<p>Thank you! Your message has been sent.</p>"; 
} 

?> 

<form id="" name="" action="<?php echo get_permalink(); ?>" method="post"> 
    <div class="form-note">We will contact you back within 24 hours with the availability and the estimated cost.</div> 
    <br/> 

    <div class="section-heading"><h6>Your Details</h6></div> 
    <div class="label-input-wrapper"> 
     <div class="form-label">Title</div> 
     <div class="form-input"> 
      <select name="name_title" class="name-title-input"> 
      <option value="" selected="selected">Select Title</option> 
      <option value="Mr">Mr</option> 
      <option value="Mrs">Mrs</option> 
      <option value="Miss">Miss</option> 
      <option value="4">Ms</option> 
      <option value="5">Dr</option> 
      <option value="6">Professor</option> 
      <option value="7">The Rt Revd Dr</option> 
      <option value="8">The Most Revd</option> 
      <option value="9">The Rt Revd</option> 
      <option value="10">The Revd Canon</option> 
      <option value="11">The Revd</option> 
      <option value="12">The Rt Revd Professor</option> 
      <option value="13">The Ven</option> 
      <option value="14">The Most Revd Dr</option> 
      <option value="16">Rabbi</option> 
      <option value="17">Canon</option> 
      <option value="18">Dame</option> 
      <option value="19">Chief</option> 
      <option value="20">Sister</option> 
      <option value="21">Reverend</option> 
      <option value="22">Major</option> 
      <option value="23">Other</option> 
      <option value="24">Cllr</option> 
      <option value="25">Sir</option> 
      <option value="26">Rt Hon Lord</option> 
      <option value="27">Rt Hon</option> 
      <option value="28">The Lord </option> 
      <option value="29">Viscount</option> 
      <option value="30">Viscountess</option> 
      <option value="31">Baroness</option> 
      <option value="32">Captain</option> 
      <option value="33">Master</option> 
      <option value="34">Very Revd</option> 
      <option value="35">Lady</option> 
      <option value="38">MP</option> 
      <option value="39">King of Kings and Lord of Lords</option> 
      <option value="40">Conquering Lion of the Tribe of Judah</option> 
      <option value="41">Elect of God and Light of this World</option> 
      <option value="42">His Own Divine Majesty</option> 
      <option value="43">First Ancient King of Creation</option> 
      <option value="44">King Alpha</option> 
      <option value="45">Queen Omega</option> 
      <option value="46">Beginning with Our End and First with Our Last</option> 
      <option value="47">Protector of All Human Faith</option> 
      <option value="48">Ruler of the Universe</option> 
      <option value="49">Dude</option> 
      <option value="50">Mx (gender-netural)</option> 
      </select> 
     </div> 
    </div> 

    <div class="label-input-wrapper"> 
     <div class="form-label">Name</div><div class="form-input"><input type="text" name="sendername"/></div> 
    </div> 

    <div class="label-input-wrapper"> 
     <div class="form-label">E-Mail</div><div class="form-input"><input type="email" name="senderEmail" pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,4}$" required /></div> 
    </div> 

    <div class="label-input-wrapper special"> 
     <div class="form-label">Write us in details if you have any special requirement</div><div class="form-input">​<textarea name="message"></textarea></div> 
    </div> 

    <input type="submit" value="Submit" name="submit"> 
</form> 

那麼它發送到我的電子郵件與這些輸入值。 現在我想在提交表單並且表單應該消失後顯示成功消息。我嘗試了幾個通過Google搜索得到的代碼示例,但是我無法將其實施到我的代碼中。 我該怎麼做?

[編者的話]第一次回覆] 謝謝你。但它說的語法錯誤。

<?PHP 
if($_POST["submit"]) { 
    $recipient="[email protected]"; 
    $subject="Arugambay Bookings : New reservations request"; 
    $sender=$_POST["sendername"]; 
    $senderEmail=$_POST["senderEmail"]; 
    $message=$_POST["message"]; 

    $mailBody="Name: $sender\nEmail: $senderEmail\n\n$message"; 

$mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>"); 

if ($mail_sent) { 
?> 

    <p>Mail sent</p> 

<?php 
} else { 
?> 


<form id="" name="" action="<?php echo get_permalink(); ?>" method="post"> 
    <div class="form-note">We will contact you back within 24 hours with the availability and the estimated cost.</div> 
    <br/> 
    <div class="section-heading"><h6>Your Details</h6></div> 

    <div class="label-input-wrapper"> 
     <div class="form-label">Name</div><div class="form-input"><input type="text" name="sendername"/></div> 
    </div> 

    <div class="label-input-wrapper"> 
     <div class="form-label">E-Mail</div><div class="form-input"><input type="email" name="senderEmail" pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,4}$" required /></div> 
    </div> 

    <div class="label-input-wrapper special"> 
     <div class="form-label">Write us in details if you have any special requirement</div><div class="form-input">​<textarea name="message"></textarea></div> 
    </div> 

    <input type="submit" value="Submit" name="submit"> 
</form> 

<?php 
} 
?> 
+0

你忘了關閉該HTML部分你的第一個「如果」,當你改變了你代碼 – Sugar 2014-09-02 06:41:53

+0

哦..是的..現在它的工作就像一個魅力..謝謝你... – 2014-09-02 06:48:52

回答

2

mail將成功返回true,否則爲false,只是檢查這個值就知道你要dispaly

$mail_sent = mail(...); 

if ($mail_sent) { 
?> 

    <p>Mail sent</p> 

<?php 
} else { 
?> 

    <form> 
    ... 

<?php 
} 
?> 
+0

我正要寫同樣的東西:) – Khushboo 2014-09-02 06:12:07

+0

感謝你@ClémentMalet你能看看我編輯的答案嗎? – 2014-09-02 06:37:13

相關問題