How to Include Forms in AMP (amp-form)

The amp-form extension allows you to create forms <form> to submit input fields in an AMP document.

Required Scripts

Before creating a form, you must include the required script.

<script async custom-element="amp-form" src=""></script>

For AMP Mustache to render success or error responses in the form:

<script async custom-template="amp-mustache" src=""></script>



It indicates where to display the form response after submitting the form. The value must be _blank or _top.


It specifies a server endpoint to handle the form input. The value must be an https URL (absolute or relative) and must not be a link to a CDN.

The target and action attributes are only used for non-xhr GET requests. When action-xhr is not provided, AMP makes a GET request to the action endpoint and uses target to open a new window (if _blank).


It specifies a server endpoint to handle the form input and submit the form via XMLHttpRequest (XHR). An XHR request (sometimes called an AJAX request) is where the browser would make the request without a full load of the page or opening a new page.

This attribute is required for method=POST, and is optional for method=GET. The value for action-xhr can be the same or a different endpoint than action.

enctype (optional)

The enctype attribute specifies how form-data should be encoded before sending it to the server via the method=POST submission. The default encoding is set to multipart/form-data.

Not Allowed in AMP

  • <input type=button>, <input type=image>
  • Most of the form-related attributes on inputs including: form, formaction, formtarget, formmethod and others.


<form method="post" action-xhr="" target="_top">
  <input type="text" name="name" required>
  <input type="email" name="email" required>
 <input type="submit" value="Subscribe">

Success and Error Response

You can render success or error responses in your form by using amp-mustache, or success responses through data binding with amp-bind.


It can be used to display a success message if the response is successful (has a status of 2XX).


It can be used to display a submission error if the response is unsuccessful (does not have a status of 2XX).


It can be used to display a message when the form is submitting. The template for this attribute has access to the form's input fields for any display purposes.

<form ...>
  <input type="text" name="firstName" />
<div submitting>
 <template type="amp-mustache">
 Form submitting... Thank you for waiting {{name}}.
<div submit-success>
 <template type="amp-mustache">
 Success! Thanks {{name}} for subscribing! Please check your email {{email}} to confirm!.
<div submit-error>
 <template type="amp-mustache">
  Oops! {{name}}, {{message}}.

You need to provide a valid JSON object for responses to submit-success and submit-error. Both success and error responses should have a Content-Type: application/json header.

Redirecting after a submission

You can redirect users to a new page after a successful form submission by setting the AMP-Redirect-To response header and specifying a redirect URL. The redirect URL must be a HTTPS URL, otherwise AMP will throw an error and redirection won't occur.

Access-Control-Expose-Headers: AMP-Redirect-To

PHP Script for Handling AMP Form

When the form is submitted successfully it should return a JSON response.

subscribe.php File


if (!empty($_POST))
$name = $_POST['name'];
$email = $_POST['email'];

// Send email or save in database

$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('.', '-','') ."");
header("AMP-Access-Control-Allow-Source-Origin: " . $domain_url);
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
// For Redirection
// header("AMP-Redirect-To:");
// header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin");

$amp_json = json_encode($_POST);
echo $amp_json;


Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive