首页 文章

在Wordpress模板中联系表单行为不端

提问于
浏览
0

我想指出,我试图在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没有任何好处,也无法理解它 . 任何人都可以指出我哪里出错了,我将非常感激 .

1 回答

  • 0

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

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

    希望这会有所帮助 .

    EDIT:

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

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

    它对我来说非常有用 .

相关问题