与 PhP 合作的 AMP 表格 [英] AMP Form to work with PhP

查看:18
本文介绍了与 PhP 合作的 AMP 表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AMP 设置带有联系表单的页面:

表单只有 4 个字段:

  • 姓名
  • 电子邮件:
  • 我们能提供什么帮助?
  • 电话号码:

然后我希望将该信息发送到我的电子邮件地址 - 这就是我在 HTML 中的内容

 <h4>我们需要什么帮助?</h4><form class="p3 隐藏输入"方法=发布"动作-xhr="/ampmail.php"目标=_top"><div class="ampstart-input inline-block relative mb3"><输入类型=文本"名称=名称"占位符="名称..."需要>

<div class="ampstart-input inline-block relative mb3"><输入类型="电子邮件"名称=电子邮件"占位符="电子邮件...">

<div class="ampstart-input inline-block relative mb3"><输入类型=文本区域"名称=查询"placeholder="我们有什么帮助吗?">

<div class="ampstart-input inline-block relative mb3"><输入类型=电话"名称=我的电话"placeholder="联系电话">

<输入类型=提交"价值=联系我"class="ampstart-btn caps"><div 提交成功><模板类型="amp-mustache">成功!谢谢{{name}}.我们的设计专家会尽快与您联系.

<div 提交错误><模板类型="amp-mustache">错误!感谢 {{name}} 尝试<code>amp-form</code>带有错误响应的演示.

</表单>

然后在我的 ampmail.php 中,我有以下内容:

但两人似乎并没有交流.

解决方案

请确保域名 url 应该是 https

index.html :

<html ><头><meta charset="utf-8"><title>AMP 表单</title><link rel="canonical" href="https://example.com/index.html"/><script async src="https://cdn.ampproject.org/v0.js"></script><脚本异步自定义元素="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script><脚本异步自定义模板="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{从{visibility:hidden}到{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}到{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><风格放大器定制>form.amp-form-submit-success [提交成功] {颜色:绿色;}form.amp-form-submit-error [提交错误] {红色;}</风格><身体><h4>我们能提供什么帮助?</h4><form method="post" action-xhr="ampmail.php" target="_top"><div class="ampstart-input inline-block relative mb3"><输入类型=文本"名称=名称"占位符="名称..."需要>

<div class="ampstart-input inline-block relative mb3"><输入类型="电子邮件"名称=电子邮件"占位符="电子邮件...">

<div class="ampstart-input inline-block relative mb3"><输入类型=文本区域"名称=查询"placeholder="我们有什么帮助吗?">

<div class="ampstart-input inline-block relative mb3"><输入类型=电话"名称=我的电话"placeholder="联系电话">

<输入类型=提交"价值=联系我"class="ampstart-btn caps"><div 提交成功><模板类型="amp-mustache">成功!谢谢{{name}}.我们的设计专家会尽快与您联系.

<div 提交错误><模板类型="amp-mustache">错误!感谢 {{name}} 尝试<code>amp-form</code>带有错误响应的演示.

</表单>

ampmail.php :

 $name));出口;}?>

<块引用>

https://example.com/ 替换为您的域网址

I am trying to setup a page with a contact form using AMP:

The form has only 4 fields:

Then I want that info to be sent to my email address - this is what I have in the HTML

    <h4>How can we help?</h4>
<form class="p3 hide-inputs"
  method="post"
  action-xhr="/ampmail.php"
  target="_top">
  <div class="ampstart-input inline-block relative mb3">
    <input type="text"
      name="name"
      placeholder="Name..."
      required>
  </div>
   <div class="ampstart-input inline-block relative mb3">
    <input type="email"
      name="email"
      placeholder="Email...">
  </div>
     <div class="ampstart-input inline-block relative mb3">
    <input type="text-area"
      name="enquiry"
      placeholder="How can we help?">
  </div>
  <div class="ampstart-input inline-block relative mb3">
    <input type="tel"
      name="my_tel"
      placeholder="Contact Number">
  </div>
  <input type="submit"
    value="Contact Me"
    class="ampstart-btn caps">
      <div submit-success>
    <template type="amp-mustache">
      Success! Thanks {{name}}. Our design specialists will contact you shortly.
    </template>
  </div>
  <div submit-error>
    <template type="amp-mustache">
      Error! Thanks {{name}} for trying the
      <code>amp-form</code> demo with an error response.
    </template>
  </div>
</form>

Then in my ampmail.php, I have the following:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['my_tel'];
$formcontent=" From: $name 
 Phone: $my_tel";
$recipient = "admin@example.com";
$subject = "Contact Form";
$mailheader = "From: $email 
";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

But it seems the two are not communicating.

解决方案

Please make sure that domain url should be https

index.html :

<!doctype html>
<html amp>
    <head>
        <meta charset="utf-8">
        <title>AMP Form</title>
        <link rel="canonical" href="https://example.com/index.html" />
        <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>
        <style amp-custom>
            form.amp-form-submit-success [submit-success] {
                color: green;
            }
            form.amp-form-submit-error [submit-error] {
                color: red;
            }
        </style>
    </head>
    <body>
        <h4>How can we help?</h4>
        <form method="post" action-xhr="ampmail.php" target="_top">
          <div class="ampstart-input inline-block relative mb3">
    <input type="text"
      name="name"
      placeholder="Name..."
      required>
  </div>
   <div class="ampstart-input inline-block relative mb3">
    <input type="email"
      name="email"
      placeholder="Email...">
  </div>
     <div class="ampstart-input inline-block relative mb3">
    <input type="text-area"
      name="enquiry"
      placeholder="How can we help?">
  </div>
  <div class="ampstart-input inline-block relative mb3">
    <input type="tel"
      name="my_tel"
      placeholder="Contact Number">
  </div>
  <input type="submit"
    value="Contact Me"
    class="ampstart-btn caps">
      <div submit-success>
    <template type="amp-mustache">
      Success! Thanks {{name}}. Our design specialists will contact you shortly.
    </template>
  </div>
  <div submit-error>
    <template type="amp-mustache">
      Error! Thanks {{name}} for trying the
      <code>amp-form</code> demo with an error response.
    </template>
  </div>
        </form>
    </body>
</html>

ampmail.php :

<?php
if(!empty($_POST)){
$name=$_POST['name'];
$email = $_POST['email'];
$phone = $_POST['my_tel'];
$enquiry = $_POST['enquiry'];
$formcontent=" From: $name 
 Phone: $my_tel";
$recipient = "admin@example.com";
$subject = $enquiry;
$mailheader = "From: $email 
";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

       $domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
        header("Content-type: application/json");
        header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Allow-Origin: ". str_replace('.', '-','https://example.com') .".cdn.ampproject.org");
        header("AMP-Access-Control-Allow-Source-Origin: " . $domain_url);
        header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
        header("AMP-Redirect-To: https://example.com/index.html");
        header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin"); 
        echo json_encode(array('name' => $name));
        exit;
}
?>

Replace https://example.com/ to your domain url

这篇关于与 PhP 合作的 AMP 表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆