【发布时间】: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>
一旦我知道这个简单的表格有效,我就可以真正完成联系部分并发送电子邮件。
我错过了什么吗?
谢谢
【问题讨论】:
-
这里是 WordPress 中一步一步的guide for creating AMP contact forms 的链接。但是,它不一定只适用于 WP。