0

我无法获得在我的 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 数据进一步自定义感谢页面,但是我最终陷入了这个重定向问题。

4

1 回答 1

1

一种简单的方法如下。

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

<?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(); ?>

And change your form tag to this:

<form id="adoptApp" action="http://www.bar.org/thanks/" method="post">
于 2013-02-13T16:21:41.537 回答