在 PHP.Gt 中的 运行 的什么地方放置程序代码片段?

Where do I put a procedural code snippet to run in PHP.Gt?

我已经保存了本教程中的代码:http://myphpform.com/final-form.php 表单提交时应该会发送一封电子邮件。

我想在一个简单的联系页面中使用它。这是标记:

<main role="content">
    <section>
        <header>
            <h1>Contact</h1>
        </header>
        <section role="contact-us">
            <form action="/Script/contact.php" method="post">
                <label for="name">Full name</label>
                <input type="text" name="yourname" placeholder="Name..." id="name">
                <label for="email" name="email">Email address</label>
                <input type="text" placeholder="you@email.com" id="email">
                <textarea placeholder="Your comments..." rows ="5" name="comment-text" name="comments"></textarea>
                <input type="submit" value="Send" name="submit">
            </form>
        </section>
    </section>
</main>

PHP应该去哪里,是否需要以任何方式转换?

要向 PHP.Gt 应用程序添加代码,请使用 PHP.Gt 中的页面逻辑对象。页面逻辑 PHP 在特定页面的上下文中执行,并为您的页面代码提供面向对象的入口点。

您提供的 link 中的代码使用程序 PHP,因此需要放入 class 中才能使用。

附带说明一下,您的 HTML 表单不需要在 action 属性中包含任何内容。如果没有 action 属性,它将 post 到当前页面,这就是你的逻辑所在。

假设您当前的标记位于 src/Page/contact.html,在 /src/Page/contact.php 创建一个 PHP 文件并在下面添加基本页面逻辑 class:

<?php
namespace App\Page;

class Contact extends \Gt\Page\Logic {

public function go() {
}

}#

HTML 文件(页面浏览量)和 PHP 代码(页面逻辑)之间的 link 的解释可在文档中找到:https://github.com/BrightFlair/PHP.Gt/wiki/Pages


放置在 go() 方法中的任何逻辑都将在呈现页面之前执行,因此这正是您需要放置来自 link 的电子邮件脚本的地方 post编辑。

需要对代码进行一些操作才能使其面向对象,但这是您要实现的目标的简化示例:

go() {

if(!isset($_POST["submit"])) {
    // If the form isn't submitted, do not continue.
    return;
}

mail("your-email@example.com", "Contact form message", $_POST["comment-text"]);
header('Location: /thanks');

}

程序示例中的函数 posted 可以作为私有方法简单地附加到 Logic 对象,尽管我会借此机会使用适当的验证技术更新它们,例如本机 filter_var 函数。

示例中的 show_error 函数从 PHP 回显 HTML,这与强大的 separation of concerns enforced by PHP.Gt, but the Hello, you tutorial 显示了如何使用页面逻辑来操作页面上的内容- 这是在 show_error 方法中输出错误消息的方法。