2013-04-14 76 views
0

我想在給我發電子郵件後重定向用戶。
代碼將運行,除非我在email()-方法之後(例如header()-方法)放置了某些東西。
任何提示?php代碼中的標題重定向不起作用

function process() 
{ 
    $msg = "Form Contents: \n\n"; 
    foreach($this->fields as $key => $field) 
     $msg .= "$key : $field \n"; 

    $to = '[email protected]'; 
    $subject = 'thesubject'; 
    $from = $this->fields['email']; 

    mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); 

    header('Location: thanks.html'); 
} 
+0

你會得到什麼錯誤? –

+0

我曾經在D:\ xampp \ php \ logs \ php_error_log – spamup

+0

的D:\ xampp \ htdocs \ process_form.php中得到PHP Parse錯誤:語法錯誤,意外'header'(T_STRING),期望函數(T_FUNCTION)電子郵件發送沒有任何錯誤?如果您使用@mail(...)進行嘗試,該怎麼辦? – djot

回答

-1

標題後('location:....');你應該添加一個退出;否則,腳本會一直運行直到結束。

header('Location: thanks.html'); 
exit; 
+0

我也試過這個:沒有機會。什麼都不會發生。 – spamup

+0

無論如何你應該添加退出;) – djot

+0

這是完整的編碼: [pastebin](http://pastebin.com/UhTS1aKP) – spamup

0

您的位置應該是絕對的,而不是相對的。

header("Location: /myPath/thanks.html"); 
+0

也試過這個: – spamup

3

當我遇到類似的問題,它通常是在每行的末端缺少分號,或如這裏的情況下,丟失的括號中,因爲foreach碼塊應該由擋着{}。請參閱documentation

0
<?php 
header("Location: http://www.example.com/"); /* Redirect browser */ 

/* Make sure that code below does not get executed when we redirect. */ 
exit; 
?> 

在我的情況下,有那些引號。那樣做了。