首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP使用AMP-html创建联系人表单

使用PHP使用AMP-html创建联系人表单
EN

Stack Overflow用户
提问于 2017-04-04 14:48:48
回答 1查看 4.9K关注 0票数 0

我一直试图创建一个联系形式使用AMP-html与php。我就是不能让它起作用。当我单击该按钮时,错误消息将显示在amp-胡子模板中(但没有name字段),但我无法看到实际的错误是什么。

我一直在看放大器文件和这个问题,但它不能工作。安培表格连同邮递递交。我用它作为代码的基础,我无法让它工作。我已经将成功模板移到表单中,并添加了错误模板。也许我应该就这个问题发表意见,但由于这个问题得到了公认的答案,而且我仍然有问题,我认为我应该提出一个新的问题。

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

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

代码语言:javascript
复制
<?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>

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

我有遗漏什么吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-04 18:34:25

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

看起来,您只需更改action-xhr="#"以指向实际路径或PHP页面的完整地址,例如action-xhr="//localhost:8080/contact.amp.html"。安培强制执行特别的规则,使您的网页更快,在本例中,改进安全做法在网上。

要查看有效amp-form标记的示例,试着通过示例查看AMP上的表单演示。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43210589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档