表单成功后php重定向

时间:2011-06-03 20:21:38

标签: php

使用php脚本允许人们将他们的电子邮件添加到邮件列表中。成功将电子邮件写入.txt文档后,它会在白页上显示成功消息。我希望它重定向到我设计的页面,看起来像网站的其余部分。我在其他地方读过我需要使用头函数,但由于我的代码的前一部分包含输出,我无法弄清楚放在哪里。

我认为这是相关的代码:

// Write to file if email doesn't already exist
    if ($emailexists != "yes") {

        // Write to the list and give success message
        fwrite($fh, "$_POST[email]\n");
        $msg = "Your e-mail address $EmailFix was successfully added to the list!";
    }

    // Close the list
    fclose($fh);
}

然后这个:

// Show success message if there was one and end script, and no errors were encountered
} elseif (isset($msg)) {
echo "<b>" . $msg . "</b>\n";
echo "</body>\n";
echo "</html>\n";
exit;
}

3 个答案:

答案 0 :(得分:2)

尝试使用

header("Location: http://...");

header("Location: filename.php");

在调用标题函数之前确保没有HTML输出!

答案 1 :(得分:1)

您是否希望此消息仍然显示,然后将其重定向到其他位置?或者您是否想要一起丢弃此消息并将它们引导到具有类似消息的其他页面?

您可以按原样显示该页面,并包含另一个回显以触发重定向:

echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"2;URL=/someotherpage.php\">";

答案 2 :(得分:0)

如果您之前的代码包含代码,则可以使用javascript重定向:

window.location = "YOUR_URL_HERE";

您应该向用户说明他/她将被重定向。