2016-08-09 130 views
0

當我提交使用此代碼表單提交 - 不刷新/隱藏股利後提交

PHP

if(empty($errors)) 
{ 
$to = $myemail; 
$email_subject = "Contact form submission: $name"; 
$email_body = "You have received a new message. ". 
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message"; 

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address"; 

mail($to,$email_subject,$email_body,$headers); 

//redirect to the 'thank you' page 
header('Location: index.html'); 
} 

我希望它做的是重定向我回到同一頁的形式留不清爽在同一頁上,然後隱藏在div ..

HTML

  <div class="footer"> 
      <div id="containerfooter"> 
       <form method="POST" name="contactform" action="contact-form-handler.php" class="NewsLetter" class="NL__field NL__email"> 
       <p> 
       <label for='name'>Your Name:</label> <br> 
       <input type="text" name="name" class="NL__field NL__email"> 
       </p> 
       <p> 
       <label for='email'>Email Address:</label> <br> 
       <input type="text" name="email"> <br> 
       </p> 
       <p> 
       <label for='message'>Message:</label> <br> 
       <input type="text" name="message"> 
       </p> 
       <input type="submit" value="Submit"><br> 
       </form> 
      </div> 
     </div> 

JQuery的

 $('form').submit(function(){ 
     $('#containerfooter').hide(); 
    }); 
+1

你需要使用AJAX。 https://learn.jquery.com/ajax/ – chris85

回答

1

形式VS AJAX

形式允許你把用戶輸入的數據並將其發送到處理另一個PHP文件。然而,當提交表單時,導航移動到其他PHP頁面(在表單標籤的action=""屬性指定的一個:

<form method="POST" name="contactform" action="contact-form-handler.php" class="NewsLetter" class="NL__field NL__email"> 

在上面的標籤,在點擊提交按鈕,瀏覽器導航到contact-form-handler.php

AJAX的發明是爲了解決這個問題,讓您的數據發送到第二個PHP文件,和(可選)接收不同的數據/ HTML回不改變或刷新當前頁面

這裏有一些非常簡單的AJAX例子。這並不困難 - 只需將示例複製到您的服務器並使其在您的服務器上工作即可。和他們一起玩。花費15分鐘時間可以爲您節省時間小時。

AJAX request callback using jQuery

注:在這個時代,它幾乎從來沒有必要使用形式。一切都可以通過AJAX來完成 - 這很簡單,爲什麼不呢?

注2:當使用AJAX與<form></form>標籤,你必須使用e.preventDefault()保持表單標籤從導航從當前頁面(表單標籤的默認操作)的距離:

$('myButton').submit(function(evt){ 
    evt.preventDefault(); 
    //the rest of your js code, including ajax code block