2017-04-04 49 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