AMP表格与PhP合作

时间:2017-05-02 03:23:53

标签: php amp-html

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

表单只有4个字段:

  • 名称
  • 电子邮件:
  • 我们如何帮助?
  • 电话号码:

然后我希望将这些信息发送到我的电子邮件地址 - 这就是我在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>

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

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

但似乎两人没有沟通。

2 个答案:

答案 0 :(得分:4)

  

请确保域名网址应为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 \n Phone: $my_tel";
$recipient = "admin@example.com";
$subject = $enquiry;
$mailheader = "From: $email \r\n";
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;
}
?>
  

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

答案 1 :(得分:1)

您应该在AMP-Access-Control-Allow-Source-Origin文件中使用PHPaction-xhr应该ampmail.php而不是/ampmail.php

以下代码适用于AMP验证:

index.html

<!doctype html>
<html amp>
    <head>
        <meta charset="utf-8">
        <title>AMP Form</title>
        <link rel="canonical" href="http://localhost/amp_test/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">
            Name: <input type="text" name="name" required><br/>
            Email: <input type="email" name="email" required><br/>
            Enquiry: <textarea name="enquiry" maxlength="10000" required></textarea><br/>
            Contact: <input type="tel" name="my_tel" required><br/>
            <input type="submit" value="Contact Me">
            <div submit-success>
                <template type="amp-mustache">
                    Submission successful! Thanks {{name}} for contacting.
                </template>
            </div>
            <div submit-error>
                <template type="amp-mustache">
                    Submission failed!
                </template>
            </div>
        </form>
    </body>
</html>

ampmail.php

<?php
header('AMP-Access-Control-Allow-Source-Origin: http://localhost');
$name=$_POST['name'];
$email = $_POST['email'];
$phone = $_POST['my_tel'];
$formcontent=" From: $name \n Phone: $my_tel";
$recipient = "admin@freelanceexpress.co.za";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$response = array('name' => $name);
echo json_encode($response);