一页网站联系表单问题

时间:2013-06-22 21:31:09

标签: php forms contact

我有一个单页组合与联系表格,我一直在尝试解决这个问题,但没有成功。我不是PHP专家所以我会尽力解释。基本上我所拥有的是来自word press的联系模板文件,我过去曾使用过,但我想做的是(如果可能的话)在我的投资组合中实现。我的主要问题是,当我在浏览器中预览我的网站时,它会显示表单上的PHP代码。

<?php
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                // Check fields for errors

                if (empty($_POST["txtName"])) {
                    $errors["txtName"] = "Please enter your name.";
                }

                if (empty($_POST["txtPhone"])) {
                    $errors["txtPhone"] = "Please enter your phone number.";
                }


                if (empty($_POST["txtEmail"])) {
                    $errors["txtEmail"] = "Please enter your email address.";
                } else {
                    if (!eregi('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST["txtEmail"])))) {
                        $errors["txtEmail"] = "Please provide a valid email address.";
                    }

                }

                if (count($errors) < 1) {

                        $to = "test-email@gmail.com";
                        $subject = 'Bave Designs Contact';
                        $headers = "From:" . $_POST["txtEmail"] . "\r\n";
                        $headers .= "MIME-Version: 1.0\r\n";
                        $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
                        $message = '<html><body style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">';
                        $message .= '<p><strong>Name:</strong> ' . $_POST["txtName"] . '<br />

                                    <strong>Phone:</strong> ' . $_POST["txtPhone"] . '<br />
                                    <strong>Email:</strong> ' . $_POST["txtEmail"] . '<br />

                                    <strong>Message:</strong><br />' . $_POST["txtComment"] . '</p>
                                    </body>
                                    </html>';
                        if ( !mail($to,$subject,$message,$headers) ) {

                            $errors["send"] = "There was a problem sending your message.  Please try again.";
                        }

                } 
            }
            <div id="contact-form">
                    <?php
                if (count($errors) > 0) {
                    echo '<ul style="color:red; padding:0 0 18px 22px;">';
                    foreach ($errors as $error) {
                        echo "<li>" . $error . "</li>";
                    }
                    echo "</ul>";
                }
                ?>
                <?php if ($_SERVER['REQUEST_METHOD'] == "POST" && count($errors) < 1) { ?>
            <p class="success-message"><?php _e('Thank you! Your message has been sent.'); ?></p>
            <?php } else { ?>

            <form id="contact1" method="post" action="#message" class="contact-form">
                <p><label for="txtName">Name</label>
                <input type="text" id="txtName" name="txtName" class="field" size="40" value="<?php echo $_POST['txtName'] ?>" /></p>

                <p><label for="txtPhone">Phone</label>
                <input type="text" id="txtPhone" name="txtPhone" class="field" size="40" value="<?php echo $_POST['txtPhone'] ?>" /></p>

                <p><label for="txtEmail">Email</label>
                <input type="text" id="txtEmail" name="txtEmail" class="field" size="40" value="<?php echo $_POST['txtEmail'] ?>" /></p>

                <p><label for="txtComment">Message</label>
                <textarea name="txtComment" id="txtComment" class="field" cols="40" rows="10"><?php echo $_POST['txtComment'] ?></textarea></p>

                <p><input type="submit" id="btnSubmit" name="btnSubmit" class="button" value="Submit" /></p>

            </form>
            <?php } ?>
        </div><!-- CONTACT FORM ENDS-->

我将wp_mail功能切换为mail,我知道这是正确的。

任何帮助都将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

  

我的主要问题是,当我在浏览器中预览我的网站时,它会显示出来   表单上的PHP代码。

因此,如果我理解正确,您的PHP代码将显示在您的页面中?

如果是这种情况,我建议您尽快联系您的虚拟主机提供商,因为您的Apache配置似乎存在很大问题。