2017-07-27 57 views
1

提交按鈕在contact-us.html,代碼在contact-us.php用php重定向到上一頁

我試過,沒有運氣使用header('Location: example'); ...(我嘗試使用網頁的網址,以及:header('Location: http://www.example.com/contact-us.html');

有什麼想法?

<?php 

if($_POST["submit"]) { 

    $recipient="[email protected]"; 
    $subject="Form to email message"; 
    $sender=$_POST["firstname"]; 
    $senderlast=$_POST["lastname"]; 
    $senderemail=$_POST["senderemail"]; 
    $message=$_POST["message"]; 
    $mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message"; 

    header('Location: /contact-us.html'); /*Something Wrong?*/ 

    mail($recipient, $subject, $mailBody, "From: $sender <$senderemail>") 
} 
+0

你想把用戶發回給聯繫人us.html?你發送電子郵件後的頁面? –

+0

是的!當按鈕被按下時,它進入到PHP頁面並停留在那裏 –

+0

郵件是否發送?可能是'if($ _ POST ['submit'])'是'false'並且頭文件從不設置。 – ImClarky

回答

0

您是否試圖尋找引薦者? PHP有一個功能。

header('Location: ' . $_SERVER['HTTP_REFERER']) 

這意味着如果它來自cnotact.html,它會看看它是如何到達並引用回來的。如果您在另一頁上有另一個聯繫表單,則易於使用。

我需要補充的是,雖然這可能無法通過安全頁面工作。 (所以如果你通過https運行它),它可能會被劫持(尤其是如果瀏覽器沒有發送請求)。

+1

這是由用戶代理設置的。並非所有的用戶代理都會設置它,有些提供了將HTTP_REFERER修改爲功能的功能。總之,它不能真正被信任。 –

+0

我知道這一點,(因此我修改了答案)但這個問題的litteral回答只是 – Dorvalla

-2
header('Location: contact-us.html'); /*Something Wrong? 
+0

使用它沒有/ –

+0

不幸的是它仍然無法工作... –

+0

這並不提供答案這個問題。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/16845979) – sarcom

0

以下是我會做:

我建議使用$ _SESSION來存儲先前的網址是這樣的:

page1.php中

<?php 
    $url     = 'http://'. $_SERVER[HTTP_HOST]. $_SERVER[REQUEST_URI]; 
    $_SESSION['previous'] = $url; 

使page2.php

<?php 
    if ($_SESSION['previous'] !== '') { 
     header('Location: '. $_SESSION['previous']); 
    } 

$ _SESSIO N種類似餅乾的作品,但更好。更容易使用,因爲它只是使用陣列 - 更多信息可以在這裏找到:http://uk1.php.net/manual/en/reserved.variables.session.php

0

你確定你有一個按鈕,submit名稱屬性?如果是的話,也許試試這個:

<?php 
if($_POST["submit"]) { 

    $recipient="[email protected]"; 
    $subject="Form to email message"; 
    $sender=$_POST["firstname"]; 
    $senderlast=$_POST["lastname"]; 
    $senderemail=$_POST["senderemail"]; 
    $header = "MIME-Version: 1.0" . "\r\n"; 
    $header .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
    $header .= 'From: $sender <$senderemail>' . "\r\n"; 
    $message=$_POST["message"]; 
    $mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message"; 


    if(mail($recipient, $subject, $mailBody, $header)){ 

     header('Location:contact-us.html'); /*Something Wrong?*/ 
    } 
} 

,如果沒有那麼你的代碼永遠不會進入,如果塊

+0

我不認爲這是這種情況..我收到所有的電子郵件.. –

0

嘗試使用JavaScript window.location重定向 注:包裝window.location.href('contact-us.html');到腳本標籤

例如:

echo "<script>window.location.href('contact-us.html');</script>";