表单成功后php重定向

发布于 2024-11-11 17:03:32 字数 752 浏览 0 评论 0原文

使用 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;
}

Using php script to allow people to add their email to a mailing list. When it successfully writes the email to the .txt doc it displays a success message on a white page. I want it to redirect to a page I've designed to look like the rest of the site. I've read elsewhere that I need to use the header function, but I can't figure out where to put it since the preceding portion of my code contains outputs.

I think this is the relevant code:

// 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);
}

Then this:

// 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是暖光i 2024-11-18 17:03:32

尝试使用

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

header("Location: filename.php");

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

Try using

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

or

header("Location: filename.php");

Make sure there's no HTML output before calling the header function!

走过海棠暮 2024-11-18 17:03:32

您是否希望此消息仍然显示,然后将其重定向到其他地方?或者您是否想一起丢弃此消息并将他们定向到具有类似消息的不同页面?

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

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

Do you want this message to still show and THEN redirect them somewhere else? Or do you want to discard this message all together and direct them to a different page that will have a similar message?

You could show that page as is, and include another echo to trigger a redirect:

echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"2;URL=/someotherpage.php\">";
北斗星光 2024-11-18 17:03:32

如果您之前的代码包含无法使用 javascript 重定向的代码:

window.location = "YOUR_URL_HERE";

您应该通知用户他/她将被重定向。

If your previous code contains code than you can use javascript redirection:

window.location = "YOUR_URL_HERE";

You should norify the user the user that s/he will be redirected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文