首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >联络表格不起作用

联络表格不起作用
EN

Stack Overflow用户
提问于 2016-11-12 03:20:23
回答 3查看 88关注 0票数 0

当提交我的表单时,我会得到404页。有些东西不见了,或者错了。这是wordpress的网站。我将电子邮件插入wordpress的选项中,没有发生任何事情。我看到了很多堆叠溢出的问题,但都帮不了我。我不想使用插件。

代码语言:javascript
复制
<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代码:

代码语言:javascript
复制
<?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>';
    }
}
    }
?>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-17 15:40:51

首先,谢谢您的答复。

这里的问题是php mail()。我希望我能帮助别人做这条线。

为了得出这一结论,以下是以下步骤:

  • 用同样的问题尝试了我在堆栈溢出中看到的所有东西;
  • 安装检查电子邮件插件
  • 我没有收到任何邮件,所以我联系了我的托管服务,他们说他们不会让php邮件()避免垃圾邮件。

解决方案 --当这种情况发生时,这是最好的解决办法。

票数 0
EN

Stack Overflow用户

发布于 2016-11-12 06:43:31

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

代码语言:javascript
复制
    <?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并创建新页面(标题可能是联系人页面等),从页面模板下拉(页面内容的右侧)中选择页面模板“联系人我们”,发布页面并查看页面。

希望能帮上忙。

票数 0
EN

Stack Overflow用户

发布于 2016-11-12 09:56:42

在实际操作中使用文件位置url:

代码语言:javascript
复制
action="`<?php echo get_template_directory()'contact.php';?>`"

尝尝这个。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40559258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档