forms - AMP表单提交重定向或响应

标签 forms amp-html

早上好,
我在测试AMP页面上有一个表单(已通过AMP验证),并且该表单有效:我收到带有结果的电子邮件(我使用php进行处理)。
我不知道(我做了一些尝试,但是我想我仍然想念一个例子)让AMP页面正确响应的语法(现在我收到“订阅失败!”但我确实收到了电子邮件)或提交后重定向。
这是我的例子:

AMP page with form(提交后我收到表单结果,但是我不知道如何重定向或获取“订阅成功!”消息)

Non AMP page with form(我收到表单结果,并且可以正确重定向)

action-xhr文件目标代码:
Code of the php here in txt format(处理表单结果的文件)

任何示例对我来说都是很好的,也仅关于重定向。
谢谢

最佳答案

您可以在成功提交后重定向用户,但是只有在将值提交到安全URL (like https://www.example.com)时才能完成。

带有AMP表单的HTML

<!doctype html>
<html amp lang="en">
  <head>
    <meta charset="utf-8">
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <title>Hello, AMPs</title>
    <link rel="canonical" href="http://example.ampproject.org/article-metadata.html" />
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    <script type="application/ld+json">
      {
        "@context": "http://schema.org",
        "@type": "NewsArticle",
        "headline": "Open-source framework for publishing content",
        "datePublished": "2015-10-07T12:02:41Z",
        "image": [
          "logo.jpg"
        ]
      }
    </script>
    <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>
    <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
  </head>
  <body>
    <h1>Hello World!</h1>
      <form target="_top" action-xhr="https://test.php" method="post" name="test">
        <input type="text" name="name" value="ABRA KA DABRA!">
        <input type="submit"/>
    </form>
  </body>
</html>

PHP中的提交请求处理程序代码
<?php
if(!empty($_POST))
{
    $name = $_POST['name'];

    /*/ this is the email we get from visitors*/
    $domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
    $redirect_url = 'https://example.com/thank-you';

    /*//-->MUST BE 'https://';*/
    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: ".$domain_url);


    /*/ For Sending Error Use this code /*/
    if(!mail("email@example.com" , "Test submission" , "email: $name <br/> name: $name" , "From: $name\n ")){
        header("HTTP/1.0 412 Precondition Failed", true, 412);

        echo json_encode(array('errmsg'=>'There is some error while sending email!'));
        die();
    }
    else
    {
        /*/--Assuming all validations are good here--*/
        if( empty($redirect_url))
        {
            header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
        }
        else
        {
            header("AMP-Redirect-To: ".$redirect_url);
            header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin");        }
            echo json_encode(array('successmsg'=>$_POST['name'].'My success message. [It will be displayed shortly(!) if with redirect]'));
        die();
    }
}?>

关于forms - AMP表单提交重定向或响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43422257/

相关文章:

javascript - 如何在我的网站中嵌入 AMP 页面?

html - 使用 <button type ="submit"> 代替 &lt;input type ="submit"> 提交表单,有问题吗?

PHP/MySQL : Getting form information, 未按计划工作

javascript - 带有用于选择字段集的复选框选项的 HTML 表单

mysql - 将大量用户信息存储到一个 MySQL 表中?

javascript - 单击按钮时自动聚焦到输入(适用于 AMP 文章)

amp-html - 我想在 ampstory 中使用 amp-youtube 标签

c# - AMP 页面未在我的开发站点中重定向 [非 Wordpress]

amp-html - amphtml 链接标签可以使用相对 href 而不是绝对的吗?

html - CSS 表单设计帮助