我可能在这里错过了一些非常简单的东西,或者在我的代码中有一个小错字,但是我为我建立的一个WordPress网站创建的PHP表单不会发送。如果表单中存在错误,验证将起作用(除了您输入的名称外),但是当您正确填写表单并单击提交时,它将转到其用于处理的页面,并显示“对不起,没有帖子符合你的标准。'。另外邮件不会被发送。WordPress的PHP表单不会发送
编辑
链接到开发站点是http://dev.garethdaine.com/inkframe/。
处理页面确实存在。这里是我的代码:
表单代码
<form name="quick-contact" class="quick-contact" method="post" action="<?php bloginfo('url'); ?>/get-in-touch/">
<h3>Request a Call Back</h3>
<h4><label for="name">Name</label></h4>
<input id="name" name="name" type="text" />
<h4><label for="email">Email</label></h4>
<input id="email" name="email" type="text" />
<h4><label for="phone">Phone</label></h4>
<input id="phone" name="phone" type="text" />
<input id="submit" type="submit" value="Submit" />
<input type="hidden" name="submitted" id="submitted" value="true" />
<input type="hidden" name="required" id="required" class="required" />
</form>
PHP代码
<?php
/*
* Template Name: Contact
*/
if(isset($_POST['submitted'])) {
$to = get_option('admin_email');
if(trim($_POST['name']) === '') {
$nameError = 'You did not enter your name.';
$hasError = true;
} else {
$name = trim($_POST['name']);
}
if(trim($_POST['email']) === '') {
$emailError = 'You did not 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['phone']) === '') {
$phoneError = 'You did not enter your telephone number.';
$hasError = true;
} else if(!is_numeric($_POST['phone'])) {
$phoneError = 'You have entered an invalid phone number.';
$hasError = true;
} else {
$phone = trim($_POST['phone']);
}
if(!empty($_POST['required'])) {
$requiredError = 'You appear to be a robot. If you are human, please try again.';
$hasError = true;
}
if(!isset($hasError)) {
$subject = 'Call Back Request from ' . $name;
$body = "Name: $name \n\nEmail: $email \n\nPhone: $phone";
$headers = 'From: ' . $name . ' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($to, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php get_header(); ?>
<div class="content" role="main">
<?php if (have_posts()) while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php if(isset($emailSent) && $emailSent == true) { ?>
<p><strong>Your message has been sent successfully. Someone will be in touch shortly. Thanks.</strong></p>
<?php the_content(); ?>
<?php } else { ?>
<?php if(isset($hasError)) { ?>
<p class="error">Sorry, an error has occured.<p>
<ul>
<?php if($nameError != '') { ?>
<li>
<span class="error"><?php echo $nameError; ?></span>
</li>
<?php } ?>
<?php if($emailError != '') { ?>
<li>
<span class="error"><?php echo $emailError; ?></span>
</li>
<?php } ?>
<?php if($phoneError != '') { ?>
<li>
<span class="error"><?php echo $phoneError; ?></span>
</li>
<?php } ?>
<?php if(!empty($requiredError)) { ?>
<li>
<span class="error"><?php echo $requiredError; ?></span>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php the_content(); ?>
<?php } ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
愚蠢的问题:使用定义的联系人模板的GET式触摸页? – maiorano84
是的,它使用了正确的contact.php模板。 –
我注意到,当您直接导航到“取得联系”页面(不提交表单)时,页面内容呈现没有问题。但是,当表单被提交时,不能找到内容。如果你注释掉'if(isset($ _POST ['submitted'])''块之间的所有内容,会发生什么?内容在提交表单时是否呈现?如果您在该块中放置回声语句,回声语句是否会触发? – maiorano84