AMP新闻稿表格回复问题

时间:2017-09-27 11:20:38

标签: php amp-html

我正在尝试使用Reference Link实施AMP简报订阅表单。提交表单后,在服务器端,我使用以下代码来处理请求并返回响应:

服务器端脚本:

<?php 
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");
$data = array('name'=>$_POST['name'],'email'=>$_POST['email']);
echo json_encode($data);exit;   
?>

AMP FORM HTML

<form method="post"
  class="p2"
  action-xhr="https://www.example.com/request.php"
  target="_top">
  <div class="ampstart-input inline-block relative m0 p0 mb3">
    <input type="text"
      class="block border-none p0 m0"
      name="name"
      placeholder="Name..."
      required>
    <input type="email"
      class="block border-none p0 m0"
      name="email"
      placeholder="Email..."
      required>
  </div>
  <input type="submit"
    value="Subscribe"
    class="ampstart-btn caps">
  <div submit-success>
    <template type="amp-mustache">
      Success! Thanks {{name}} for trying the
      <code>amp-form</code> demo! Try to insert the word "error" as a name input in the form to see how
      <code>amp-form</code> handles errors.
    </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>

请求完成后。它始终显示我的HTML表单模板的submit-success部分。我的主要问题是如何在服务器端返回submit-error返回上述表单的name部分,以及如何在服务器端处理请求,以便它可以显示success或{{ 1}}消息分别?

2 个答案:

答案 0 :(得分:1)

根据服务器响应的状态代码呈现submit-successsubmit-error div。对于错误响应,状态代码需要在4XX或5XX范围内。

答案 1 :(得分:0)

感谢@SebastianBenz给我一个错误响应状态的提示,即 4XX 5XX 。不过我已阅读amp-from,但我对错误响应下提到的 2XX 感到困惑。 submit-success将呈现状态为2XX的所有回复,即200,201,201等。

以下是我完整的工作通讯代码:

AMP HTML FORM:

<form method="post"
  class="p2"
  action-xhr="https://www.example.com/request.php"
  target="_top">
  <div class="ampstart-input inline-block relative m0 p0 mb3">
    <input type="text"
      class="block border-none p0 m0"
      name="name"
      placeholder="Name..."
      required>
    <input type="email"
      class="block border-none p0 m0"
      name="email"
      placeholder="Email..."
      required>
  </div>
  <input type="submit"
    value="Subscribe"
    class="ampstart-btn caps">
  <div submit-success>
    <template type="amp-mustache">
      Success! Thanks {{name}} for trying the
      <code>amp-form</code> demo! Try to insert the word "error" as a name input in the form to see how
      <code>amp-form</code> handles errors.
    </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>

PHP SCRIPT(request.php):

<?php 
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");
/* Return error if Posted name is sanchit or do your logic */ 
if($_POST['name'] == 'sanchit'){
    /* Return Error*/   
    header("HTTP/1.0 412 Precondition Failed", true, 412);
    $data = array('name'=>$_POST['name'],'email'=>$_POST['email'],'msg'=>'Sorry '.$_POST['name'].'! cannot access this form.');
    echo json_encode($data);exit;
}else{
    /* Return Success */
    header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
    $data = array('name'=>$_POST['name'],'email'=>$_POST['email']);
    echo json_encode($data);exit;   
}
exit;

<强>更新

替换以下行

header("Access-Control-Allow-Origin: *.ampproject.org");

header("Access-Control-Allow-Origin: ". str_replace('.', '-','https://www.example.com') .".cdn.ampproject.org");