尝试如下设置所有字段:
步骤1。
//if form submitted
if(isset($_POST[\'submitted\'])) {
// use this for all fields
if(trim($_POST[\'contactname\']) === \'\') {
$nameError = \'Please enter your name.\';
$hasError = true;
} else {
$name = trim($_POST[\'contactName\']);
}
// this is for email
if(trim($_POST[\'email\']) === \'\') {
$emailError = \'Please enter your email address.\';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\\.[a-z]{2,4}$/i", trim($_POST[\'email\']))) {
$emailError = \'You entered an invalid email address.\';
$hasError = true;
} else {
$email = trim($_POST[\'email\']);
}
if(trim($_POST[\'message\']) === \'\') {
$messageError = \'Please enter a message.\';
$hasError = true;
} else {
if(function_exists(\'stripslashes\')) {
$message= stripslashes(trim($_POST[\'message\']));
} else {
$message= trim($_POST[\'message\']);
}
}
if(!isset($hasError)) {
$emailTo = get_option(\'tz_email\');
if (!isset($emailTo) || ($emailTo == \'\') ){
$emailTo = get_option(\'admin_email\');
}
$subject = \'[PHP Snippets] From \'.$name;
$body = "Name: $name \\n\\nEmail: $email \\n\\nMessage: $message";
$headers = \'From: \'.$name.\' <\'.$emailTo.\'>\' . "\\r\\n" . \'Reply-To: \' . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
步骤2。更正错误
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="thanks">
<p>Thanks, your email was sent successfully.</p>
</div>
<?php } else { ?>
<?php the_content(); ?>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="correct_errors">Please correct the highlighted errors and try again..<p>
<?php } ?>
第3步。您的表单
<form action="<?php the_permalink(); ?>" id="contactform" method="post">
<p class="error_s"><?php if($nameError != \'\') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?></p>
<p><label for="contactName" class="cContact">Name</label> <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST[\'contactName\'])) echo $_POST[\'contactName\'];?>" class="required requiredField inputas" style="width:190px;" /></p>
....do the same for other inputs...
<p class="error_s">
<?php if($messageError != \'\') { ?>
<span class="error"><?=$messageError;?></span>
<?php } ?>
</p>
<label for="message" class="cContact">Message</label> <textarea name="comments" id="message" class="requiredField" cols="48" rows="10" ><?php if(isset($_POST[\'message\'])) { if(function_exists(\'stripslashes\')) { echo stripslashes($_POST[\'message\']); } else { echo $_POST[\'comments\']; } } ?></textarea>
<input type="submit" name="submit" class="submitt" value="Send your message" style="cursor:pointer">
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
即使看起来不一样,也很容易做到。你所需要的就是尝试更多!
如果仍要重定向到其他页面,请使用以下选项:
if (mail($to, $subject, $body, $headers))
{
// This needs to change to the permalink of a page with the ID of 8
wp_redirect( \'http://yourdomain.com/succes/\', 301 ); exit;
}
else
{
// This needs to change to the permalink of a page with the ID of 12
wp_redirect( \'http://yourdomain.com/error/\', 301 ); exit;
}
}