简体   繁体   English

WordPress网站上的php联系表单无法正常运行

[英]php contact form on wordpress site not running correctly

I am developing on XAMPP with a wordpress install. 我正在使用Wordpress安装在XAMPP上进行开发。

My form looks good everything is how I want it but when I click the submit button it loads and index.php aka my websites home page. 我的表单看起来很不错,这是我想要的一切,但是当我单击“提交”按钮时,它就会加载,并且index.php也就是我的网站主页。

so when I click submit it goes from 所以当我点击提交时,它来自

http://localhost/WP/contact/ HTTP://本地主机/ WP /联系人/

to

http://localhost/WP/contact/index.php HTTP://localhost/WP/contact/index.php

It shouldn't work without any info in it though... I tried to follow a tutorial so I'm just lost and I'm still learning the basics of JS and PHP. 但是,如果没有任何信息,它应该不起作用...我试图按照一个教程进行操作,所以我迷路了,并且仍在学习JS和PHP的基础知识。

Here is my code for the form 这是我的表格代码

<?php
                            if(isset($_POST['submit'])){
                                $name = $_POST['name'];
                                $email = $_POST['email'];
                                $message = $_POST['message'];
                                $from = 'From: $email'; 
                                $to = 'alexgray410@gmail.com'; 
                                $subject = 'Hello';

                                $body = "From: $name\n E-Mail: $email\n Message:\n $message";
                            }
                            ?>

                            <header class="body">

                            </header>

                            <section class="body">
                                <form method="post" action="index.php" class="contact">

                                    <label class="contact">Name</label>
                                    <input name="name" placeholder="Type Here" class="contact">

                                    <label class="contact">Email</label>
                                    <input name="email" type="email" placeholder="Type Here" class="contact">

                                    <label class="contact">Message</label>
                                    <textarea name="message" placeholder="Type Here" class="contact"></textarea>
                                    <label class="contact">*What is 2+2? (Anti-spam)</label>
                                    <input name="human" placeholder="Type Here" class="contact">        
                                    <input id="submit" name="submit" type="submit" value="Submit" class="contact">

                                </form>
                            </section>

                        <?php
                        if(isset($_POST['submit'])){
                            $name = $_POST['name'];
                            $email = $_POST['email'];
                            $message = $_POST['message'];
                            $from = 'From: TangledDemo'; 
                            $to = 'demo@tangledindesign.com'; 
                            $subject = 'Hello';
                            $human = $_POST['human'];

                            $body = "From: $name\n E-Mail: $email\n Message:\n $message";

                            if ($_POST['submit']) {
                                if ($name != '' && $email != '') {
                                    if ($human == '4') {                 
                                        if (mail ($to, $subject, $body, $from)) { 
                                        echo '<p>Your message has been sent!</p>';
                                    } else { 
                                        echo '<p>Something went wrong, go back and try again!</p>'; 
                                    } 
                                } else if ($_POST['submit'] && $human != '4') {
                                    echo '<p>You answered the anti-spam question incorrectly!</p>';
                                }
                                } else {
                                    echo '<p>You need to fill in all required fields!!</p>';
                                }
                            }
                        }
                        ?>

you have not added headers, see the correct php mail format below. 您尚未添加标题,请参见下面的正确php邮件格式。

$to=$to_email;
$subject ="Your subject";
$website="example.com";

// Set headers
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$website.'<'.$sender_email. ">\r\n";

// Format message
$contactMessage =
"Your message";
// Send email
$mail_sent=mail($to, $subject, $contactMessage, $headers);

I assume that the website is in WordPress 我认为该网站使用WordPress

at the end of if(isset($_POST['submit'])){} place header redirection of php if(isset($_POST['submit'])){}的末尾放置php的标头重定向

    <?php
                        if(isset($_POST['submit'])){
                            $name = $_POST['name'];
                            $email = $_POST['email'];
                            $message = $_POST['message'];
                            $from = 'From: TangledDemo'; 
                            $to = 'demo@tangledindesign.com'; 
                            $subject = 'Hello';
                            $human = $_POST['human'];

                            $body = "From: $name\n E-Mail: $email\n Message:\n $message";

                            if ($_POST['submit']) {
                                if ($name != '' && $email != '') {
                                    if ($human == '4') {                 
                                        if (mail ($to, $subject, $body, $from)) { 
                                        echo '<p>Your message has been sent!</p>';
                                    } else { 
                                        echo '<p>Something went wrong, go back and try again!</p>'; 
                                    } 
                                } else if ($_POST['submit'] && $human != '4') {
                                    echo '<p>You answered the anti-spam question incorrectly!</p>';
                                }
                                } else {
                                    echo '<p>You need to fill in all required fields!!</p>';
                                }
                            }
                        }
header("location:".get_the=permalink(<page_ID_of_CONTACT US page>));
                        ?>
    }

header() function should be last of everything but before closing the if condition header()函数应该是所有函数的最后一个函数,但是在关闭if条件之前

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

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