WordPress:提交后的PHP表单重定向

时间:2013-02-13 16:14:04

标签: php wordpress web

我无法获得我在WordPress网站上创建的自定义表单,以便在提交后重定向到“谢谢”页面。表单操作调用php函数,该函数首先通过电子邮件发送表单内容。电子邮件很好。

我的表单标记设置为:

<form id="adoptApp" action="<?php formMailer(); ?>" method="post">

我的formMailer函数包含:

if ( empty($_POST["applicantName"]) ) {
    return;
}

$to = "foo@bar.org";

if ( empty( $_POST["preferredPup"] ) ) {
    $subject = "Addoption Application";
} else {
    $subject = "Addoption Application for " . $_POST["preferredPup"];
}

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Foo <foo@bar.org>" . "\r\n";
$headers .= "Cc: " . $_POST["applicantEmail"] . "\r\n";

$application = adoptionApplicationEmail();

mail( $to, $subject, $application, $headers );

在php邮件功能之后,我试图使用以下方法进行页面重定向。所有这些都导致表单只是重新加载,空白。

// Method #1
$redirectURL = "http://www.bar.org/thanks/";
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=' . $redirectURL . '">';

// Method #2
wp_safe_redirect('http://www.bar.org/thanks/');

// Method #3
header("Location: http://www.bar.org/thanks/");

最后感谢页面的代码(如果它有用)。

<?php /* Template Name: FormSubmitted_ThankYou */ ?>
<?php get_header(); ?>
<?php $myScripts = $_SERVER['DOCUMENT_ROOT'] . 'path/to/scripts.php' ;
    include $myScripts;
?>

<p style="padding: 5px">
<b>Thank you!</b><br />You application has been submitted.  We will review it as soon as possible.<br />
In the meantime if you have any questions you may email us at <a href="mailto:foo@bar.org">foo@bar.org</a>
</p>

<?php get_footer(); ?>

我的目的是使用一些$ _POST数据进一步自定义感谢页面,但是我最终陷入了这个重定向问题。

1 个答案:

答案 0 :(得分:0)

执行此操作的一种简单方法如下。

将您的电子邮件代码添加到感谢页面,如下所示:

<?php /* Template Name: FormSubmitted_ThankYou */

if ( empty($_POST["applicantName"]) ) {
    exit();
}

$to = "foo@bar.org";

if ( empty( $_POST["preferredPup"] ) ) {
    $subject = "Addoption Application";
} else {
    $subject = "Addoption Application for " . $_POST["preferredPup"];
}

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Foo <foo@bar.org>" . "\r\n";
$headers .= "Cc: " . $_POST["applicantEmail"] . "\r\n";

$application = adoptionApplicationEmail();

mail( $to, $subject, $application, $headers );

get_header();

$myScripts = $_SERVER['DOCUMENT_ROOT'] . 'path/to/scripts.php' ;
include $myScripts;
?>

<p style="padding: 5px">
<b>Thank you!</b><br />You application has been submitted.  We will review it as soon as possible.<br />
In the meantime if you have any questions you may email us at <a href="mailto:foo@bar.org">foo@bar.org</a>
</p>

<?php get_footer(); ?>

将表单标记更改为:

<form id="adoptApp" action="http://www.bar.org/thanks/" method="post">