使用 AMP-html 和 PHP 创建联系表单
Creating a contact form using AMP-html with PHP
我一直在尝试使用 AMP-html 和 php 创建联系表单。我就是无法让它工作。当我单击该按钮时,错误消息显示在 amp-mustache 模板中(但没有名称字段)- 但我看不到实际错误是什么。
我一直在查看 amp 文档和这个问题,但无法正常工作。 。我已将其用作我的代码的基础,但我无法使其正常工作。我已将成功模板移到表单中并添加了错误模板。也许我应该对那个问题发表评论,但由于它有一个公认的答案,而且我仍然有问题,我想我应该提出一个新问题。
我尝试使用 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 沙箱中,我在 Chrome devtools 控制台
中发现了以下错误
看来您只需要更改 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-html 和 php 创建联系表单。我就是无法让它工作。当我单击该按钮时,错误消息显示在 amp-mustache 模板中(但没有名称字段)- 但我看不到实际错误是什么。
我一直在查看 amp 文档和这个问题,但无法正常工作。
我尝试使用 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 沙箱中,我在 Chrome devtools 控制台
中发现了以下错误看来您只需要更改 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.