0

我想指出,我并不是真正的开发人员,而是设计师。我正在尝试在 Wordpress 模板文件中添加自定义联系表单。它的工作原理基于Cats Who Code: How to create an built-in contact form for your Wordpress theme 中的教程。

我已将联系表格修改为以下内容:

  • 名称(文本字段)
  • 公司(文本字段)
  • 地址(文本区域)
  • 电子邮件地址(文本字段)
  • 电话(文本字段)
  • 移动(文本字段)
  • 查询(文本区)

但是,当我将它集成到自定义模板中并将其放在 Wordpress 安装中时,它只会给我一个空白页面,其中包含空白代码。我想知道为什么会发生这种情况,当我从模板中删除联系表时,没有联系表一切都很好。下面是整个模板代码。

<?php
if(isset($_POST['submitted'])) {
if(trim($_POST['name']) === '') {
    $nameError = 'Please enter your name.';
    $hasError = true;
} else {
    $name = trim($_POST['name']);
}
if(trim($_POST['company']) === '') {
    $companyError = 'Please enter your company name.';
    $hasError = true;
} else {
    $company = trim($_POST['company']);
}
if(trim($_POST['address']) === '') {
    $addressError = 'Please enter your address.';
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $address = stripslashes(trim($_POST['address']));
    } else {
        $address = trim($_POST['address']);
    }
}
    if(trim($_POST['email']) === '')  {
    $emailError = 'Please enter your email address.';
    $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']);
}
if(trim($_POST['telephone']) === '') {
    $telephoneError = 'Please enter your telephone number.';
    $hasError = true;
} else {
    $telephone = trim($_POST['telephone']);
}
if(trim($_POST['mobile']) === '') {
    $mobileError = 'Please enter your mobile phone number.';
    $hasError = true;
} else {
    $mobile = trim($_POST['mobile']);
}
if(trim($_POST['enquiry']) === '') {
    $enquiryError = 'Please enter a message.';
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $enquiry = stripslashes(trim($_POST['enquiry']));
    } else {
        $enquiry = trim($_POST['enquiry']);
    }
}

if(!isset($hasError)) {
    $emailTo = get_option('tz_email');
    if (!isset($emailTo) || ($emailTo == '') ){
        $emailTo = get_option('admin_email');
    }
    $subject = '[Blue Doors] From '.$name;
    $body = "Name: $name \n\nCompany: $company \n\nAddress: $address \n\nEmail: $email \n\nTel: $telephone \n\nMobile: $mobile \n\nDetails of Enquiry: $enquiry";
    $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;
}
} ?>

<?php get_header(); ?>

<section class="content">
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

    <header class="content-title">
        <h1><?php the_title(); ?></h1>
    </header>

        <article class="content-body">  
            <?php the_post_thumbnail(); ?>
            <?php the_content(); ?>
        </article>

        <article class="content-body">

        <?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>Sorry, an error occured.<p>
                        <?php } ?>

            <form action="<?php the_permalink(); ?>" id="contactform" method="post">
                <ul>
                    <li>
                        <label>What is your name?</label>
                        <input type="text" name="name" id="name" value="<?php if(isset($_POST['name'])) echo $_POST['name'];?>" class="required"/>
                        <?php if($nameError != '') { ?>
                                <span class="error"><?=$nameError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What is your company's name?</label>
                        <input type="text" name="company" id="company" value="<?php if(isset($_POST['company'])) echo $_POST['company'];?>" class="required"/>
                    </li>
                    <li>
                        <label class="address">What is your address?</label>
                        <textarea name="address" id="address" rows="5" cols="30" class="required"><?php if(isset($_POST['address'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['address']); } else { echo $_POST['address']; } } ?></textarea>
                            <?php if($addressError != '') { ?>
                                <span class="error"><?=$addressError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What is your email address?</label>
                        <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required" />
                            <?php if($emailError != '') { ?>
                                <span class="error"><?=$emailError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What is your telephone no?</label>
                        <input type="text" name="telephone" id="telephone" value="<?php if(isset($_POST['telephone'])) echo $_POST['telephone'];?>" class="required" />
                            <?php if($nameError != '') { ?>
                                <span class="error"><?=$telephoneError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What is your mobile no?</label>
                        <input type="text" name="mobile" id="mobile" value="<?php if(isset($_POST['mobile'])) echo $_POST['mobile'];?>" class="required" />
                            <?php if($mobileError != '') { ?>
                                <span class="error"><?=$mobileError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What would you like to discuss about with Blue Doors?</label>
                        <textarea name="enquiry" id="enquiry" rows="8" cols="30" class="required"><?php if(isset($_POST['enquiry'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['enquiry']); } else { echo $_POST['enquiry']; } } ?></textarea>
                            <?php if($enquiryError != '') { ?>
                                <span class="error"><?=$enquiryError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <button type="submit" class="submitbutton">Submit your enquiry</button>
                    </li>
                </ul>
                <input type="hidden" name="submitted" id="submitted" value="true" />
            </form>
        </article>

    <?php endwhile; endif; ?>

</section>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

我试图弄清楚它在哪里崩溃,但我对 PHP 不是很了解,也无法理解它。谁能指出我哪里出错了,将不胜感激。

4

1 回答 1

0

您已将联系表单代码放入循环中<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

尝试将整个联系表单代码从循环外部<?php if(isset($emailSent) && $emailSent == true) { ?>开始。</form>即把代码部分放在这一行之后<?php endwhile; endif; ?>

希望这会有所帮助。

编辑:

您在代码上缺少一个花括号。更新以下代码部分。我只在最后一行添加了一个大括号。

<?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>Sorry, an error occured.<p>
                        <?php } } ?>

它对我来说非常有用。

于 2012-05-09T15:44:51.763 回答