amp-form post action-xhr response stripping image tag

373 Views Asked by At

I am trying to display an image from an amp-form response. When I inspect the dom on reply the amp-img tag seems to get stripped.

form.php

<!doctype html>
<html amp lang="en">
<head>
<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.2.js"></script>
</head>
<body>
<form method="post" action-xhr="/amp/test.post.form.endpoint.php">
  <input type="text" name="name" placeholder="Name" required>
  <input type="email" name="email" placeholder="Email" >
  <input type="submit" value="Submit">
  <div submit-success>
    <template type="amp-mustache">

      {{{message}}}
    </template>
  </div>
  <div submit-error>
    <template type="amp-mustache">

      {{message}}
    </template>
  </div>
</form>

</body>
</html>

test.post.form.endpoint.php

<?php

const SUCCESS_CODE = 200;
const SUCCESS_MESSAGE = '<div><p>Start Test<amp-img src="welcome.jpg" alt="Welcome" height="400" width="800"></amp-img>End Test</p></div>';
const ERROR_CODE = 400;
const ERROR_MESSAGE = 'There are some missing values, please review your form.';

$host = isset($_SERVER['HTTP_X_FORWARDED_HOST'])
  ? $_SERVER['HTTP_X_FORWARDED_HOST']
  : $_SERVER['HTTP_HOST'];
header('Content-Type: application/json');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin');
header('AMP-Access-Control-Allow-Source-Origin: https://' . $host);


if (!isset($_POST['name']) || empty($_POST['name'])) {
  $error = [
    'message' => ERROR_MESSAGE
  ];

  $json = json_encode($error);
  http_response_code(ERROR_CODE);
  die($json);
}

$success = [
    'message' => SUCCESS_MESSAGE
];
$json = json_encode($success);
http_response_code(SUCCESS_CODE);
die($json);

?>

resulting in

<div id="rendered-message-amp-form-0" i-amphtml-rendered=""><p>Start TestEnd Test</p></div>

How can I get the amp-img tag sent through with the rest of html code and what is causing it to be removed?

1

There are 1 best solutions below

0
On

Your header content type is not allowing the tags header('Content-Type: application/json'); . Please change the template as below. and Just return image name as a response.

<div submit-success>
    <template type="amp-mustache">
<div><p>Start Test<amp-img src={{{ message }}} alt="Welcome" height="400" width="800"></amp-img>End Test</p></div>
   </template>
  </div>
  <div submit-error>
    <template type="amp-mustache">

      <div><p>Start Test<amp-img src={{{ message }}} alt="Welcome" height="400" width="800"></amp-img>End Test</p></div>

    </template>
  </div>