使用AMP-html和PHP创建联系表 [英] Creating a contact form using AMP-html with PHP

查看:96
本文介绍了使用AMP-html和PHP创建联系表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用AMP-html和php创建联系表.我就是无法正常工作.当我单击按钮时,该错误消息从amp-mustache模板显示(但没有名称字段)-但我看不到实际的错误是什么.

I have been trying to create a contact form using AMP-html with php. I just cannot get it to work. When I click the button, the error message shows from the amp-mustache template (but without the name field) - but I cannot see what the actual error is.

我一直在查看放大器文档和此问题,但无法正常工作. 带邮递提交的AMP表单.我将其用作代码的基础,但我无法使其正常工作.我已经将成功模板移到了表单中,并添加了错误模板.也许我应该对此问题发表评论,但是由于它的答案已经被接受,而且仍然存在问题,我认为我应该打开一个新问题.

I have been looking at the amp documentation and this question, but cannot it get to work. AMP form submitting with post. I have used this as a basis for my code, and I cannot get it to work. I have moved the success template into the form and added the error template. Perhaps I should have commented on that question, but as it has an accepted answer, and I still have issues, I thought I should open a new question.

我花了一段时间尝试使用http,但是现在意识到我需要使用https,所以我尝试使用此域,但是没有运气.

I was for a while trying with http, but now realise I need to use https so am trying on a domain with this, but no luck.

除了域名外,我的代码完全如下.

My code is exactly as follows except for the domain names.

<?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>

一旦我知道这个简单的表格可以工作,我就可以完成联系部分并发送电子邮件.

Once I know this simple form is working, I can actually complete the contact section and send the email.

我想念什么吗?

谢谢

推荐答案

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

I ran your code in a PHP sandbox and I found the following error in the Chrome devtools console

看起来您只需要更改action-xhr="#"以指向您的PHP页面的实际路径或完整地址,例如action-xhr="//localhost:8080/contact.amp.html". AMP强制执行特殊规则,以使您的页面更快,或者在这种情况下为改进网络上的安全做法.

It looks like you just need to change action-xhr="#" to point to the actual path or full address of your PHP page e.g. action-xhr="//localhost:8080/contact.amp.html". AMP enforces special rules that make your page faster or, in this case, improve security practices on the web.

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

To see examples of valid amp-form markup, try looking at the form demos on AMP by Example.

这篇关于使用AMP-html和PHP创建联系表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆