2015-02-11 53 views
1

我保存了本教程中的代码:http://myphpform.com/final-form.php当表单提交时应该发送电子邮件。我在哪里放置了一个程序代码片段来运行在PHP.Gt中?

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

<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="[email protected]" 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应该去哪里,并且它需要以任何方式进行转换?

回答

0

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

您提供的链接中的代码使用了过程式PHP,因此需要将其放入类中才能使用。

请注意,您的HTML表单不需要action属性中的任何内容。如果没有action属性,它会发布到当前页面,这是您的逻辑所在。

假设你的当前标记所在的src/Page/contact.html,在/src/Page/contact.php创建一个PHP文件,并添加裸机页面逻辑如下类:

<?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("[email protected]", "Contact form message", $_POST["comment-text"]); 
header('Location: /thanks'); 

}

过程示例中发布的函数可以简单地作为私有方法附加到逻辑对象上,但我会借此机会使用适当的验证技术(如本机filter_var功能。

例子中的show_error函数在PHP,能够避免因PHP.Gt强制强separation of concerns呼应HTML,但Hello, you tutorial显示可以使用页面逻辑来操作页面上展示内容 - 这是你如何能够输出错误消息在show_error方法中。

相关问题