【问题标题】:How to connect html code to the php code "contact form"如何将 html 代码连接到 php 代码“联系表”
【发布时间】:2019-10-08 19:16:28
【问题描述】:

我是编码新手,我想做的是制作联系表格,给自己发送电子邮件

我的问题是:如何将这两个代码连接在一起? 我想当我按下“发送按钮”从 HTML 中捕获所有字段,然后使用 PHP 代码将电子邮件发送给自己

这是我的名为 function.php 的 PHP 文件

<?php
if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $mailTo = "xxxx@xxxx.com";
    $headers = "From: ".$mailFrom;
    $txt = "You have received an e-mail from ".$name.".\n\n".$message;
    mail($mailTo, $subject, $txt, $headers);
    header("Location: index.php?mailsend");
}
?>

这是我的名为 index.html 的 html 文件

<div class="row">
    <div class="col-lg-12 m-30px-b sm-m-15px-b">
        <div class="contact-form">
            <h4 class="dark-color font-alt m-20px-b">If you need help! feel free to contact me!</h4>
            <form class="contactform" method="post">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <input id="name" name="name" type="text" placeholder="Name" class="validate form-control" required="">
                                <span class="input-focus-effect theme-bg"></span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <input id="email" type="email" placeholder="Email" name="email" class="validate form-control" required="">
                            <span class="input-focus-effect theme-bg"></span>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group">
                            <textarea id="message" placeholder="Your Comment" name="message" class="form-control" required=""></textarea>
                                <span class="input-focus-effect theme-bg"></span>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="send">
                            <button class="btn btn-theme" type="submit" name="send"> send message</button>                   
                        </div>
                        <span class="output_message"></span>
                    </div>
                </div>
            </form>
        </div>
    </div> <!-- col -->
</div>

【问题讨论】:

  • 任何关于使用 PHP 处理表单的教程都应该解释这一点。您需要在&lt;form&gt; 标签中添加action="function.php"
  • @FunkFortyNiner 这些骗子与表单中缺少的action 属性有什么关系?
  • 我没听懂,你能给我解释一下吗? @Barmar,我已经从 youtube 教程过来了,但是那家伙没有解释基本的东西,我在这里问
  • &lt;form action="function.php" class="contactform" method="post"&gt;
  • 因为@Barmar 他们的表格失败了。

标签: php html email


【解决方案1】:

您使用&lt;form&gt; 标记的action 属性将HTML 表单连接到处理它的脚本。所以你需要改变

        <form class="contactform" method="post">

        <form action="function.php" class="contactform" method="post">

没有这个,它默认发布到与包含表单的页面相同的 URL。

一种常见的风格是使用单个 PHP 脚本来创建表单并处理输入,所以他们省略了这个属性,你阅读的教程可能已经采用了这种风格。

另一个问题是提交按钮的名称是name="send",而不是name="submit"。所以你需要改变

if (isset($_POST['submit']))

if (isset($_POST['send']))

【讨论】:

  • 我将查看该链接,我使用短标签,但我的 php 在我的服务器上启用。我会检查其他选项,谢谢:)
  • 如果在服务器上启用了 PHP,您将不会在结果中显示 PHP 代码。
  • 我再次检查它说“PHP版本ea-php72”,我还检查了服务器cpanle php短标签是否启用
  • 你没有使用短标签,那有什么区别呢?如果您在浏览器中看到 PHP 脚本,则说明您没有正确启用 PHP。
  • 我没有看到任何代码,发送按钮只打开空白页面“function.php”但是是白色和空白..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-26
  • 2010-10-27
  • 2023-04-11
相关资源
最近更新 更多