RickLFK
RickLFK

Reputation: 44

WordPress: PHP Form Redirect after Submit

I am unable to get a custom form I have created on my WordPress site to redirect to a "Thank You" page after it is submitted. The form action calls a php function, which first emails the form content. The email comes through fine.

My form tag is setup as:

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

and my formMailer function contains:

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

$to = "[email protected]";

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 <[email protected]>" . "\r\n";
$headers .= "Cc: " . $_POST["applicantEmail"] . "\r\n";

$application = adoptionApplicationEmail();

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

After the php mail function I have attempted to use the following methods for page redirection. All of them result in the form simply reloading, blank.

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

And finally the code for the thank you page (in case it is helpful).

<?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:[email protected]">[email protected]</a>
</p>

<?php get_footer(); ?>

My intention is to further customize the thank you page with some of the $_POST data, however I ended up stuck at this redirection issue.

Upvotes: 0

Views: 3469

Answers (1)

Marvin Rabe
Marvin Rabe

Reputation: 4241

One easy way to do this is the following.

Add your email code to the thanks page like this:

<?php /* Template Name: FormSubmitted_ThankYou */

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

$to = "[email protected]";

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 <[email protected]>" . "\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:[email protected]">[email protected]</a>
</p>

<?php get_footer(); ?>

And change your form tag to this:

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

Upvotes: 1

Related Questions