联系表格不起作用

时间:2016-11-12 03:20:24

标签: php wordpress

提交表单时,我会收到404页面。有些东西丢失了,或者错了。 它是一个wordpress网站。我在wordpress的选项中插入了电子邮件,没有任何反应。我看到了很多堆栈溢出问题,但没有人帮助我。我不想使用插件。

<form class="form-horizontal" role="form" method="post" action="contact.php">

           <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
                <input type="text" class="form-control" id="name" name="name" placeholder="Name" value="">
           </div>
      <br/>     
           <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span>
                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
           </div>
      <br/>     
           <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span>
                <textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea>
           </div>
      <br/>     
           <div class="input-group">
                <span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span>
                <input type="text" class="form-control" id="human" name="human" placeholder="">
           </div>
    <br/>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-5">
            <input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">
            <! Will be used to display an alert to the user>
        </div>
    </div>
</form>

PHP代码:

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Contact Form'; 
        $to = 'example@domain.com'; 
        $subject = 'Contact ';

        $body = "From: $name\n E-Mail: $email\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['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam is incorrect';
        }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>

3 个答案:

答案 0 :(得分:0)

在主题中创建一个.php文件(当前主题,如果在主题主题中有子主题),然后将下面的代码放在文件中并保存。

    <?php
/*
* Template Name: Contact us
*/
get_header(); 
if (isset($_POST["submit"]))
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = intval($_POST['human']);
    $from = 'Contact Form'; 
    $to = 'example@domain.com'; 
    $subject = 'Contact ';
    $body = "From: $name\n E-Mail: $email\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['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
    {
        $errEmail = 'Please enter a valid email address';
    }
    //Check if message has been entered
    if (!$_POST['message']) 
    {
        $errMessage = 'Please enter your message';
    }
    //Check if simple anti-bot test is correct
    if ($human !== 5) 
    {
        $errHuman = 'Your anti-spam is incorrect';
    }
    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage && !$errHuman) 
    {
        if (mail ($to, $subject, $body, $from)) 
        {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } 
        else 
        {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }
}
?>
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <form class="form-horizontal" role="form" method="post" action="#">
            <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
                <input type="text" class="form-control" id="name" name="name" placeholder="Name" value="">
            </div><br/>     
            <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span>
                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
            </div><br/>     
            <div class="input-group">
                <span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span>
                <textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea>
            </div><br/>     
            <div class="input-group">
                <span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span>
                <input type="text" class="form-control" id="human" name="human" placeholder="">
            </div><br/>
            <div class="form-group">
                <div class="col-sm-10 col-sm-offset-5">
                    <input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-10 col-sm-offset-2">
                    <!-- Will be used to display an alert to the user -->
                </div>
            </div>
        </form>
    </main>
    <?php get_sidebar( 'content-bottom' ); ?>
</div>
<?php get_footer(); ?>

现在转到wp-admin并创建新页面(标题可能是联系页面等),选择页面模板&#34;联系我们&#34;从页面模板下拉列表(页面内容的右侧),发布页面并查看页面。

希望它会有所帮助。

答案 1 :(得分:0)

首先感谢您的回复。

这里的问题是php mail()。我希望我能用这个帖子帮助别人。

为了得出这个结论,这些步骤是:

  • 尝试了我在堆栈溢出中看到的所有问题都有我的问题;
  • 安装Check Email Plugin;
  • 我没有收到任何电子邮件,所以我联系我的托管服务,他们说他们不会启用php邮件()来避免垃圾邮件。

Solution - 当发生这种情况时,这是解决问题的最佳方式。

答案 2 :(得分:-1)

在操作中使用文件位置网址:

action="`<?php echo get_template_directory()'contact.php';?>`"

试试这个。