2013-12-17 97 views
0

我有一個聯繫表單,我想在某人完成並提交他們的信息後隱藏。以下是代碼。理想情況下,我想隱藏HTML字段集標籤之間的所有內容。我知道這可以用jQuery來完成,但用PHP可以嗎?使用PHP提交後隱藏聯繫表單?

<form id="contact" method="post" action="index.php"> 
    <fieldset> 
     <label for="name">Full Name<span class="red"> *</span></label> 
     <input type="text" name="name" title="Enter your name" required> 

     <label for="email">E-mail Address<span class="red"> *</span></label> 
     <input type="email" name="email" placeholder="[email protected]" title="Enter your e-mail address" required> 

     <label for="phone">Phone Number</label> 
     <input type="tel" name="phone" title="Enter your phone number" placeholder="ex. (555) 555-5555"> 

     <label for="message">Questions and Comments<span class="red"> *</span></label> 
     <textarea type="text" name="message" title="Enter your questions or comments" required></textarea>                       

     <label>What is 4+2? (Anti-spam)<span class="red"> *</span></label> 
     <input type="text" name="human" placeholder="Answer Here" title="Answer Here" required> 

     <input type="submit" name="submit" class="button" id="submit" value="Send Us A Message" title="Send us a message" /> 
     <p class="red"> 
      * Indicates the field is required. 
     </p> 
    </fieldset> 
    <!-- Contact Form Details --> 
    <?php $name = $_POST['name']; 
     $email = $_POST['email']; 
     $message = $_POST['message']; 
     $from = 'This message has been sent from your website.'; 
     $to = '[email protected]'; 
     $subject = 'From your contact form'; 
     $human = $_POST['human']; 

     $body = " From: $name\n E-Mail: $email\n Message:\n $message"; 

     if ($_POST['submit'] && $human == '6') { 
      if (mail($to, $subject, $body, $from)) { 
       echo ' 
    <h2>Thank You!</h2> 
    <span class="message"> 
     Your message has been sent! 
    </span>'; 
      } else { 
       echo ' 
    <h2>Oops!</h2> 
    <span class="message"> 
     Something went wrong, go back and try again! 
    </span>'; 
      } 
     } else if ($_POST['submit'] && $human != '6') { 
      echo ' 
      <h2>Incorrect Answer!</h2> 
    <span class="message"> 
     The correct answer is 6. Please try again. 
    </span>'; 
     } 
    ?> 
</form> 

UPDATE:嘗試一些建議後,我意識到,因爲形式提交後,網頁被重新加載,試圖掩蓋的形式是沒有意義的。你點擊提交,表單消失,頁面重新加載,表單再次出現。所以我在一個容器中包圍了from並修改了CSS,因此消息出現在表單上方,這很好。不過謝謝大家的幫忙。非常感謝!

+1

PHP是一種服務器端語言。在你的頁面被渲染的時候,PHP早已執行。這種類型的問題已經被問過很多次了。您可以發回到您的表單並以這種方式隱藏它,但它不能像JavaScript一樣交互地完成。 [這裏是一個例子](http://stackoverflow.com/questions/12219753/how-to-show-or-hide-a-form-using-php)。 [你也應該嘗試利用SO上的搜索。](http://stackoverflow.com/search?q=use+php+to+hide+form) – Buggabill

回答

0

試試這個:

if(!isset($_POST['submit'])) 
{ 
    <form id="contact" method="post" action="index.php"> 
    <!--Contents in between--> 
    </form> 
} 

也能改變你的HTML:

<form id="contact" method="post" action="index.php"> 
    <fieldset> 
     <label for="name">Full Name<span class="red"> *</span></label> 
     <input type="text" name="name" title="Enter your name" required> 

     <label for="email">E-mail Address<span class="red"> *</span></label> 
     <input type="email" name="email" placeholder="[email protected]" title="Enter your e-mail address" required> 

     <label for="phone">Phone Number</label> 
     <input type="tel" name="phone" title="Enter your phone number" placeholder="ex. (555) 555-5555"> 

     <label for="message">Questions and Comments<span class="red"> *</span></label> 
     <textarea type="text" name="message" title="Enter your questions or comments" required></textarea>                       

     <label>What is 4+2? (Anti-spam)<span class="red"> *</span></label> 
     <input type="text" name="human" placeholder="Answer Here" title="Answer Here" required> 

     <input type="submit" name="submit" class="button" id="submit" value="Send Us A Message" title="Send us a message" /> 
     <p class="red"> 
      * Indicates the field is required. 
     </p> 
    </fieldset> 
</form> 
    <!-- Contact Form Details --> 
    <?php $name = $_POST['name']; 
     $email = $_POST['email']; 
     $message = $_POST['message']; 
     $from = 'This message has been sent from your website.'; 
     $to = '[email protected]'; 
     $subject = 'From your contact form'; 
     $human = $_POST['human']; 

     $body = " From: $name\n E-Mail: $email\n Message:\n $message"; 

     if ($_POST['submit'] && $human == '6') { 
      if (mail($to, $subject, $body, $from)) { 
       echo ' 
    <h2>Thank You!</h2> 
    <span class="message"> 
     Your message has been sent! 
    </span>'; 
      } else { 
       echo ' 
    <h2>Oops!</h2> 
    <span class="message"> 
     Something went wrong, go back and try again! 
    </span>'; 
      } 
     } else if ($_POST['submit'] && $human != '6') { 
      echo ' 
      <h2>Incorrect Answer!</h2> 
    <span class="message"> 
     The correct answer is 6. Please try again. 
    </span>'; 
     } 
    ?> 

不要包括聯繫表格詳細<form>...</form>

+0

如果CAPTCHA失敗,那麼這將不起作用,我認爲。 –

0

成功時,設置一個變量:

$success = true; 

然後周圍的HTML表單:

<?php echo $success ? '<!--' : ''; ?> 
// form here... 
<?php echo $success ? '-->' : ''; ?> 
0

爲什麼不把上述表單的PHP代碼,並添加一個if條件?如果您只想在整個瀏覽器會話中只顯示一次表單,則必須使用php會話來存儲標誌。

<form id="contact" method="post" action="index.php"> 
    <fieldset> 
     <!-- Contact Form Details --> 
     <?php 
     $success = false; 
     $name = $_POST['name']; 
     $email = $_POST['email']; 
     $message = $_POST['message']; 
     $from = 'This message has been sent from your website.'; 
     $to = '[email protected]'; 
     $subject = 'From your contact form'; 
     $human = $_POST['human']; 

     $body = " From: $name\n E-Mail: $email\n Message:\n $message"; 

     if ($_POST['submit'] && $human == '6') { 
      if (mail($to, $subject, $body, $from)) { 
       $success = true; 
       echo ' 
    <h2>Thank You!</h2> 
    <span class="message"> 
     Your message has been sent! 
    </span>'; 
      } else { 
       echo ' 
    <h2>Oops!</h2> 
    <span class="message"> 
     Something went wrong, go back and try again! 
    </span>'; 
      } 
     } else if ($_POST['submit'] && $human != '6') { 
      echo ' 
      <h2>Incorrect Answer!</h2> 
    <span class="message"> 
     The correct answer is 6. Please try again. 
    </span>'; 
     } 

     if ($success) { 
     ?> 
     <label for="name">Full Name<span class="red"> *</span></label> 
     <input type="text" name="name" title="Enter your name" required> 

     <label for="email">E-mail Address<span class="red"> *</span></label> 
     <input type="email" name="email" placeholder="[email protected]" title="Enter your e-mail address" required> 

     <label for="phone">Phone Number</label> 
     <input type="tel" name="phone" title="Enter your phone number" placeholder="ex. (555) 555-5555"> 

     <label for="message">Questions and Comments<span class="red"> *</span></label> 
     <textarea type="text" name="message" title="Enter your questions or comments" required></textarea> 

     <label>What is 4+2? (Anti-spam)<span class="red"> *</span></label> 
     <input type="text" name="human" placeholder="Answer Here" title="Answer Here" required> 

     <input type="submit" name="submit" class="button" id="submit" value="Send Us A Message" title="Send us a message" /> 
     <p class="red"> 
      * Indicates the field is required. 
     </p> 
     <?php } ?> 
    </fieldset> 

</form>