【问题标题】:Where do I put a procedural code snippet to run in PHP.Gt?我在哪里放置程序代码片段以在 PHP.Gt 中运行?
【发布时间】:2015-02-11 11:34:15
【问题描述】:

我已经保存了本教程中的代码: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 email email-validation php.gt


    【解决方案1】:

    要将代码添加到您的 PHP.Gt 应用程序,您可以使用 PHP.Gt 中的页面逻辑对象。 Page Logic 是在特定页面的上下文中执行的 PHP,它为您的页面代码提供了一个面向对象的入口点。

    您提供的链接中的代码使用程序 PHP,因此需要放入一个类中才能使用。

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

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

    <?php
    namespace App\Page;
    
    class Contact extends \Gt\Page\Logic {
    
    public function go() {
    }
    
    }#
    

    文档中提供了 HTML 文件(页面视图)和 PHP 代码(页面逻辑)之间链接的说明:https://github.com/BrightFlair/PHP.Gt/wiki/Pages


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

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

    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');
    

    }

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

    示例中的 show_error 函数与 PHP 中的 HTML 相呼应,这与 PHP.Gt 强制执行的强 separation of concerns 相悖,但 Hello, you tutorial 显示了如何使用 Page Logic 操作页面上的内容 - 这是如何在show_error 方法中输出错误消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      相关资源
      最近更新 更多