2017-04-04 69 views
0

我一直在试图创建一个使用AMP-HTML用PHP接触形式的接触形式。我只是无法让它工作。当我点击按钮时,错误信息从amp-mustache模板(但没有名称字段)显示 - 但我看不到实际的错误。创建使用AMP-HTML与PHP

我一直在看放大器的文档和这个问题,但不能它的工作。 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> 

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

我错过了什么?

谢谢

+0

的[AMP形式与交提交]可能的复制(http://stackoverflow.com/questions/41346187/amp-form-submitting-with-post) –

回答

1

我跑你的代码的PHP沙盒,我发现在Chrome以下错误devtools控制台

action-xhr must start with https:// or // or be relative and served from either https or from localhost. Invalid value "#"

它看起来像你只需要改变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.

+0

谢谢 - 我现在还变得有点 - AMP存取控制允许来源-Origin标。我会研究这个错误,但你已经解决了我的原始问题。 – pbs