2013-04-24 90 views
0

由於我是PHP新手,因此遇到問題。我寫了一個簡單的聯繫表單,但在發送消息(一切正常)後,它將我帶到一個空白頁面。有什麼方法可以回到contact.html頁面並顯示消息「發送成功」?返回上一頁和消息

<?php 
    if(isset($_POST['submit'])) { 
     $to ='[email protected]'; 
     $subject =$_POST['subject']; 
     $headers = 'MIME-Version: 1.0' . "\r\n"; 
     $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; 
     $message = " 
       <html> 
        <head> 
         <title>$_POST['subject']</title> 
        </head> 
        <body> 
         <b>Person: </b> ".$_POST['name']." </br> 
         <b>Email: </b>  ".$_POST['email']." </br> 
         <b>Message: </b> ".$_POST['detail']." </br> 
        </body> 
       </html>" ; 
    mail($to, $subject, $message, $headers); 
    } 
?> 

回答

0

,所以你需要將它轉換爲PHP頁面你不能在html頁面的查詢字符串

<?php 


    header('Location: contact.php?msg=sent successfully'); 
    ?> 

contact.php page 

    <?php 

    if($_GET['msg']) 
    { ?> 
    <p> 
    Message has been sent successfully. 
    thank you. 

    </p> 

<?php } 

?> 

其他解決方案:

做出了新的一頁Thankyou.html留言和使用

<?php 


header('Location: Thankyou.html'); 
?> 
+1

由於jquery對我來說依然陌生,所以我決定採用第二種解決方案:)。簡單而奔跑!謝謝 – 2013-04-24 11:50:56

0
header("location:contact.php?msg=YOUR_MSG"); 
die(); 
1

可以使用header功能重定向。

header('Location: contact.php?msg=Mail Sent'); 
exit; 

contact.php

if(isset($_GET['msg'])){ 
    echo $_GET['msg']; 
} 
+0

非常感謝! :)它的工作 – 2013-04-24 11:49:36

+0

很高興幫助你。請不要忘記通過點擊左側的複選標記來接受我的答案。 – Rikesh 2013-04-24 11:54:44

0

在HTML文件中,您不能使用PHP所以首先改變contact.html到contact.php,則:

試試這個:

if (mail($to, $subject, $message, $headers)) { 
header("location:contact.php?msg=sent successfully"); 
} 

In contact.php

if(isset($_GET['msg'])) { 
echo $_GET['msg'] 
}