我可能在這裏錯過了一些非常簡單的東西,或者在我的代碼中有一個小錯字,但是我爲我建立的一個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