Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Contact Form Thank You Page WordPress

  • SOLVED

I am using the code below for my contact form. I would like to redirect the user to a page (thanks.html) after the form is submitted. Does anybody know what the proper code to create this redirect (after successful submission) would be and where I would insert that code?

<?php
//If the form is submitted
if(isset($_POST['submitted'])) {

//Check to see if the honeypot captcha field was filled in
if(trim($_POST['checking']) !== '') {
$captchaError = true;
} else {

//Check to make sure that the name field is not empty
if(trim($_POST['contactName']) === 'Name') {
$nameError = 'Your name is required';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) === 'Email') {
$emailError = 'Your email is required';
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['comments']) === 'Enter your message') {
$commentError = 'You must enter a message';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}

//If there is no error, send the email
if(!isset($hasError)) {

$emailTo = get_option('to_email');
$subject = 'Contact Form Submission from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments \n\nIP: ".$_SERVER['REMOTE_ADDR'];
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

mail($emailTo, $subject, $body, $headers);

if($sendCopy == true) {
$subject = 'You emailed Your Name';
$headers = 'From: Your Name <[email protected]>';
mail($email, $subject, $body, $headers);
$ip = $_SERVER['REMOTE_ADDR'];
}

$emailSent = true;
} }
} ?>



<li>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<span class="error">Message did not send. Please correct the error(s) below.</span>
<?php } ?>
</li>
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">

<li>
<input type="text" name="contactName" id="contactName" onfocus="clearDefault(this)" onblur="if(this.value=='')this.value='Name';" value="Name" class="requiredField" />

<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</li>

<li>
<input type="text" name="email" id="email" onfocus="clearDefault(this)" onblur="if(this.value=='')this.value='Email';" value="Email" class="requiredField" />
<?php if($emailError != '') { ?>
<span class="error"><?=$emailError;?></span>
<?php } ?>
</li>


<li class="textarea">

<textarea name="comments" id="commentsText" rows="1" cols="100" class="requiredField" onfocus="if(this.value==this.defaultValue)this.value=''" onblur="if(this.value=='')this.value=this.defaultValue">
<?php
if(isset($_POST['comments'])) {
if(function_exists('stripslashes')) {
echo stripslashes($_POST['comments']);
}
else { echo $_POST['comments'];
} }
else { echo 'Enter your message';
} ?>

</textarea>
<?php if($commentError != '') { ?>
<span class="error"><?=$commentError;?></span>
<?php } ?>
</li>

<li class="sendcopy">
<input type="checkbox" name="sendCopy" id="sendCopy" value="true"<?php if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true) echo ' checked="checked"'; ?> />
<label for="sendCopy">Send a copy of this email to yourself</label>
</li>


<li class="screenReader">
<label for="checking" class="screenReader">If you want to submit this form, do not enter anything in this field</label><input type="text" name="checking" id="checking" class="screenReader" value="<?php if(isset($_POST['checking'])) echo $_POST['checking'];?>" /></li>
<li class="button"><input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit">Send</button></li>
</form>

Answers (5)

2013-11-25

Arnav Joy answers:

After this line $emailSent = true;
Add following
header('location:http://www.yoursite.com/thanks.html');


siouxfan45 comments:

I received this error:
WARNING: CANNOT MODIFY HEADER INFORMATION - HEADERS ALREADY SENT BY (OUTPUT STARTED AT /HOME/JBSIMONS/PUBLIC_HTML/WP-CONTENT/PLUGINS/ALL-IN-ONE-SEO-PACK/AIOSEOP_CLASS.PHP:1128) IN /HOME/JBSIMONS/PUBLIC_HTML/WP-CONTENT/THEMES/MAG/SIDEBAR.PHP ON LINE 107


Arnav Joy comments:

try this
<?php

//If the form is submitted

if(isset($_POST['submitted'])) {



//Check to see if the honeypot captcha field was filled in

if(trim($_POST['checking']) !== '') {

$captchaError = true;

} else {



//Check to make sure that the name field is not empty

if(trim($_POST['contactName']) === 'Name') {

$nameError = 'Your name is required';

$hasError = true;

} else {

$name = trim($_POST['contactName']);

}



//Check to make sure sure that a valid email address is submitted

if(trim($_POST['email']) === 'Email') {

$emailError = 'Your email is required';

$hasError = true;

} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {

$emailError = 'You entered an invalid email address.';

$hasError = true;

} else {

$email = trim($_POST['email']);

}



//Check to make sure comments were entered

if(trim($_POST['comments']) === 'Enter your message') {

$commentError = 'You must enter a message';

$hasError = true;

} else {

if(function_exists('stripslashes')) {

$comments = stripslashes(trim($_POST['comments']));

} else {

$comments = trim($_POST['comments']);

}

}



//If there is no error, send the email

if(!isset($hasError)) {



$emailTo = get_option('to_email');

$subject = 'Contact Form Submission from '.$name;

$sendCopy = trim($_POST['sendCopy']);

$body = "Name: $name \n\nEmail: $email \n\nComments: $comments \n\nIP: ".$_SERVER['REMOTE_ADDR'];

$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;



mail($emailTo, $subject, $body, $headers);



if($sendCopy == true) {

$subject = 'You emailed Your Name';

$headers = 'From: Your Name <[email protected]>';

mail($email, $subject, $body, $headers);

$ip = $_SERVER['REMOTE_ADDR'];

}



$emailSent = true;

?>
<script>
location.href="http://www.yoursite.com/thanks.html";
</script>
<?php

} }

} ?>







<li>

<?php if(isset($hasError) || isset($captchaError)) { ?>

<span class="error">Message did not send. Please correct the error(s) below.</span>

<?php } ?>

</li>

<form action="<?php the_permalink(); ?>" id="contactForm" method="post">



<li>

<input type="text" name="contactName" id="contactName" onfocus="clearDefault(this)" onblur="if(this.value=='')this.value='Name';" value="Name" class="requiredField" />



<?php if($nameError != '') { ?>

<span class="error"><?=$nameError;?></span>

<?php } ?>

</li>



<li>

<input type="text" name="email" id="email" onfocus="clearDefault(this)" onblur="if(this.value=='')this.value='Email';" value="Email" class="requiredField" />

<?php if($emailError != '') { ?>

<span class="error"><?=$emailError;?></span>

<?php } ?>

</li>





<li class="textarea">



<textarea name="comments" id="commentsText" rows="1" cols="100" class="requiredField" onfocus="if(this.value==this.defaultValue)this.value=''" onblur="if(this.value=='')this.value=this.defaultValue">

<?php

if(isset($_POST['comments'])) {

if(function_exists('stripslashes')) {

echo stripslashes($_POST['comments']);

}

else { echo $_POST['comments'];

} }

else { echo 'Enter your message';

} ?>



</textarea>

<?php if($commentError != '') { ?>

<span class="error"><?=$commentError;?></span>

<?php } ?>

</li>



<li class="sendcopy">

<input type="checkbox" name="sendCopy" id="sendCopy" value="true"<?php if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true) echo ' checked="checked"'; ?> />

<label for="sendCopy">Send a copy of this email to yourself</label>

</li>





<li class="screenReader">

<label for="checking" class="screenReader">If you want to submit this form, do not enter anything in this field</label><input type="text" name="checking" id="checking" class="screenReader" value="<?php if(isset($_POST['checking'])) echo $_POST['checking'];?>" /></li>

<li class="button"><input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit">Send</button></li>

</form>

2013-11-25

Linda answers:

Hi, another option might be to put this after $emailSent = true;

wp_redirect( 'my-target-page-here', 301 );
exit;

2013-11-25

Francisco Javier Carazo Gil answers:

If you have problem with "header already sent" using Arnav and Linda options, use:


echo "<script>document.location.href='<?php echo home_url() ?>/my-target-page-here'</script>


siouxfan45 comments:

Where would I put this?


Francisco Javier Carazo Gil comments:

In the same place you would put header() o wp_redirect(), you have to delete it and put the echo I told you.


siouxfan45 comments:

When you say "you have to delete it" what is the it you are referring to?


Francisco Javier Carazo Gil comments:

Where have you wrote wp_redirect? You have to replace it by my code.

2013-11-25

Remy answers:

If you want to be event safer, you can use wp_safe_redirect() instead of wp_redirect()

2013-11-26

phppoet answers:

add this code after line 117

$message='<script>setTimeout(function () {
window.location.href = "http://yourdomain.com/thanks.html"; //will redirect to your blog page (an ex: blog.html)
}, 5000);
</script>';

echo $message;



where replace "5000(5 seconds )" with your desired waiting time before redirect and "http://yourdomain.com/thanks.html" with your exact thanks page address.

full code will be like this

http://pastebin.com/S8Z7b5rG

hope it works for you .