AMP Newsletter Form Response Issue

∥☆過路亽.° 提交于 2019-12-21 06:57:14

问题


I am trying to implement AMP newsletter subscription form using Reference Link. Once form is submitted, on the server side I use following code to handle the request and return response :

Server Side Script :

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

Once request is completed. It always display the submit-success part of my HTML form template. My main question is How do I show submit-error part of above form with name return from server side and how do I handle the request in server side so that it can display success or error message respectively?


回答1:


The submit-success and submit-error divs are rendered based on the status code of the server response. For error responses, the status code needs to be in the 4XX or 5XX range.




回答2:


Thank you @SebastianBenz for giving me a hint for error response status i.e 4XX or 5XX. However I have read amp-from but I was confused with 2XX mentioned under Error Response. submit-success will render for all responses that has a status of 2XX i.e 200, 201, 201 etc.

Now following is my complete working newsletter code :

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;

UPDATE

Replace following line

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

With

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


来源:https://stackoverflow.com/questions/46446610/amp-newsletter-form-response-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!