【问题标题】:Creating a contact form using AMP-html with PHP使用 AMP-html 和 PHP 创建联系表单
【发布时间】:2017-04-04 14:48:48
【问题描述】:

我一直在尝试使用 AMP-html 和 php 创建一个联系表单。我只是无法让它工作。当我单击该按钮时,错误消息从 amp-mustache 模板中显示(但没有名称字段) - 但我看不到实际错误是什么。

我一直在查看 amp 文档和这个问题,但它无法正常工作。 AMP form submitting with post。我已将此作为我的代码的基础,但我无法让它工作。我已将成功模板移到表单中并添加了错误模板。也许我应该对这个问题发表评论,但由于它有一个公认的答案,而且我仍然有问题,我想我应该提出一个新问题。

我曾经尝试过一段时间使用 http,但现在意识到我需要使用 https,所以我正在尝试使用它的域,但没有运气。

除了域名,我的代码如下。

<?php
if(isset($_POST['submitlogin']))
{
$name = isset($_POST['name']) ? $_POST['name'] : '' ;
$output = [
        'name' => $name
];
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin:https://www.example.com");
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");

echo json_encode($output);
die();

}
?>
<!doctype html>
<html amp>
<head>
    <meta charset="utf-8">
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
    <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="canonical" href="https://www.example.com/test.php"/>
    <title>AMP form</title>
</head>
<body>
<form method="post" action-xhr="#" target="_top">
    Name:<input type="text" name="name" />
    <input type="submit" name="submitlogin" value="Submit" />

    <div submit-success>
        <template type="amp-mustache">
            Success! Thanks for trying the
            <code>amp-form</code> demo! The name submitted was {{name}}
        </template>
    </div>
    <div submit-error>
        <template type="amp-mustache">
            Error! Thanks {{name}} for trying
        </template>
    </div>
</form>
</body>
</html>

一旦我知道这个简单的表格有效,我就可以真正完成联系部分并发送电子邮件。

我错过了什么吗?

谢谢

【问题讨论】:

标签: php amp-html


【解决方案1】:

我在 PHP 沙箱中运行了您的代码,并在 Chrome 开发工具控制台中发现了以下错误

看起来您只需要更改 action-xhr="#" 以指向 PHP 页面的实际路径或完整地址,例如action-xhr="//localhost:8080/contact.amp.html"。 AMP 强制执行特殊规则,使您的网页更快,或者在这种情况下,improve security practices 在网络上。

要查看有效amp-form 标记的示例,try looking at the form demos on AMP by Example.

【讨论】:

  • 谢谢 - 我现在更进一步 - AMP-Access-Control-Allow-Source-Origin 标头。我会调查这个错误,但你已经解决了我原来的问题。
猜你喜欢
  • 2017-10-06
  • 1970-01-01
  • 2012-03-08
  • 2015-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多