【问题标题】:Contact Form misbehaving in Wordpress Templates联系表格在 WordPress 模板中行为不端
【发布时间】:2012-05-18 03:01:49
【问题描述】:

我想指出,我并不是真正的开发人员,而是设计师。我正在尝试在 Wordpress 模板文件中添加自定义联系表单。它的工作原理基于Cats Who Code: How to create a 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 不是很了解,也无法理解它。谁能指出我哪里出错了,不胜感激。

【问题讨论】:

  • 您是否将模板添加到联系页面?
  • 是的,我做到了。每次我去页面时它都是空白的。如果我删除了表单,联系页面就可以正常工作。
  • 通过WordPress仪表板在您的联系页面中放置一些虚拟内容,并检查这些内容是否显示。
  • 我尝试了这种方法,它与 Lorem Ipsum 段落配合良好,无需联系表格。使用联系表,它是一个空白页。

标签: php jquery wordpress contact-form


【解决方案1】:

您已将联系表单代码放入循环 &lt;?php if(have_posts()) : ?&gt;&lt;?php while(have_posts()) : the_post(); ?&gt;

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

希望这会有所帮助。

编辑:

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

<?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 } } ?>

它对我来说非常有用。

【讨论】:

  • 我把它移到了循环之外,它仍然会导致同样的问题。出于好奇,标题和表单内部表单的 PHP 是否按预期工作?
  • 是的。至少表单应该在浏览器中呈现。让我检查一下 PHP 代码。
  • 丢失的} 是我从未见过的。 &lt;?php } } ?&gt;您的解决方案使我的联系表格有效。感谢您花时间为我解决这个问题@Libin。
猜你喜欢
  • 2014-12-14
  • 2012-11-01
  • 2013-02-17
  • 1970-01-01
  • 2017-02-11
  • 2017-01-26
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多