简体   繁体   English

Wordpress中的自定义PHP联系人表格

[英]Custom PHP contact form in wordpress

I'm reusing some code I have on static sites for a contact form. 我正在重复使用静态网站上的一些代码来联系表单。

So I imagine my problem is something wordpress related. 所以我想我的问题与wordpress有关。

I have left my other PHP code in there incase something is causing problems. 我将其他PHP代码留在了那里,以防万一引起问题。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I feel it is probably the index.php part in the form. 我觉得这可能是表单中的index.php部分。 because now my links have to include the extension for the domain. 因为现在我的链接必须包含域的扩展名。

www.richardmiddleton.me/growthitude-wordpress is the site. 网站是www.richardmiddleton.me/growthitude-wordpress。

Thanks in advance 提前致谢

PHP PHP

<?php /* Template Name: home */ ?>


<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $from = 'Form on site';
        $to = 'Richard@richardemailaddress.me'; // BUSINESS EMAIL ADDRESS
        $subject = 'Message from form';

        $body ="From: $name\n Number: $phone\n Message:\n $message";
        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['phone'] || !filter_var($_POST['phone'])) {
            $errphone = 'Please enter a valid phone number';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }

// If there are no errors, send the email
if (!$errName && !$errPhone && !$errMessage) {
    if (mail ($to, $subject, $body, $from)) {
         header("Location: success.html");
         exit();
    } else {
        header("Location: fail.html");
        exit();
    }
}
    }
?>

<?php get_header(); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php endwhile; ?>
<?php endif; ?>

<?php the_content(); ?>

HTML HTML

<form id="contact-form" role="form" method="post" action="index.php">

                    <div class="controls">
                        <div class="row">
                            <div class="col-md-6">


                                <!-- Form Name -->

                                <div class="form-group">

                                    <input type="text" class="form-control" id="name" required="required" name="name" placeholder="NAME (REQUIRED)" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                                    <?php echo "<p class='text-danger'>$errName</p>";?>
                                    <div class="help-block with-errors"></div>
                                </div>

                                <!-- END Form Name -->

                            </div>


                            <div class="col-md-6">


                                <!-- Form Number -->

                                <div class="form-group">

                                    <input type="phone" class="form-control" id="phone" required="required" name="phone" placeholder="PHONE NUMBER (REQUIRED)" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
                                    <?php echo "<p class='text-danger'>$errPhone</p>";?>
                                    <div class="help-block with-errors"></div>
                                </div>

                                <!-- END Form Number -->

                            </div>

                        </div>
                        <!-- END ROW 1 FORM -->



                        <div class="row">
                            <div class="col-md-12">


                                <!-- Form Message -->

                                <div class="form-group">

                                    <textarea class="form-control" rows="10" placeholder="TELL US ABOUT YOUR CLEANING CARE NEEDS" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                                    <?php echo "<p class='text-danger'>$errMessage</p>";?>
                                    <div class="help-block with-errors"></div>
                                </div>

                                <!-- END Form Message -->


                            </div>


                            <!-- Form Submit -->

                            <div class="col-md-12 text-center">
                                <input id="submit" name="submit" type="submit" value="SEND" class="btn btn-success btn-md">
                            </div>

                            <!-- END Form Submit -->


                        </div>


                        <!-- Form Required -->

                        <div class="row">
                            <div class="col-md-12 text-center">

                            </div>
                            <div class="form-group">
                                <div class="col-sm-12">
                                    <?php echo $result; ?>
                                </div>
                            </div>
                        </div>


                </form>

Generaly you need to use wp_mail() instead of mail() function. wp_mail()您需要使用wp_mail()而不是mail()函数。 Second thing is that WordPress have some defined POST informations and you can't use simple post name fields. 第二件事是WordPress具有一些已定义的POST信息,并且您不能使用简单的帖子名称字段。

you have post names like name, email, message, submit etc. 您有职位名称,例如姓名,电子邮件,消息,提交等。

You can't use that. 您不能使用它。 .

Replace that with some prefix like bzx_name , bzx_email , bzx_message etc. and also in POST replace like $_POST['bzx_name'] . 将其替换为一些前缀,如bzx_namebzx_emailbzx_message等,并在POST中替换$_POST['bzx_name']

Also remove index.php from action attribute inside form if you just refresh page or provide full URL if you need to go on another one. 如果您只是刷新页面,或者也需要提供完整的URL,则也可以从form内的action属性中删除index.php

This will work fine after fix. 修复后,它将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM