Do you build things with AMP? Fill out the new AMP Developer Survey!
AMP
  • email

amp-form

Introduction

The amp-form extension allows the usage of forms and input fields in an AMP document. amp-form allows HTTP and XHR (XMLHttpRequest) form submissions. An HTTP form submission loads a new page, while an XHR form submission doesn't require a page reload.

The amp-form extension also allows you to render success and error responses with the submit-success and submit-error special attributes. For details on usage, see the amp-form documentation on Success/Error response rendering.

Setup

Import the amp-form extension.

<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>

amp-mustache is needed for client-side rendering of form responses.

<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>

Form submission with client-side rendering

Use the action-xhr attribute to submit the form via XMLHttpRequest (XHR). You can use amp-mustache templates to client side render the POST response. It's possible to show custom success or error messages, using data sent by the server endpoint as JSON. For example, if the server sends {"foo": "bar"}, you can use use {{foo}} in the template to render bar.

The amp-form component displays submit-success or submit-error elements based on the response and renders the template data inside these two elements. The submit-success and submit-error elements must be direct children of form.

<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-input-text-xhr">
  <input type="text"
    name="name"
    placeholder="Name..."
    required>
  <input type="email"
    name="email"
    placeholder="Email..."
    required>
  <input type="submit"
    value="Subscribe">
  <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>

Form custom validation

The amp-form extension allows you to build your own custom validation UI by using the custom-validation-reporting, read here about the different strategies.

Use show-all-on-submit strategy to ensure all the validation messages are shown when the user submits the form.

Please enter your first and last name separated by a space (e.g. Jane Miller)
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-input-text-xhr"
  custom-validation-reporting="show-all-on-submit">
  <input type="text"
    id="show-all-on-submit-name"
    name="name"
    placeholder="Name..."
    required
    pattern="\p{L}+\s\p{L}+">
  <span visible-when-invalid="valueMissing"
    validation-for="show-all-on-submit-name"></span>
  <span visible-when-invalid="patternMismatch"
    validation-for="show-all-on-submit-name">
    Please enter your first and last name separated by a space (e.g. Jane Miller)
  </span>
  <input type="email"
    id="show-all-on-submit-email"
    name="email"
    placeholder="Email..."
    required>
  <span visible-when-invalid="valueMissing"
    validation-for="show-all-on-submit-email"></span>
  <span visible-when-invalid="typeMismatch"
    validation-for="show-all-on-submit-email"></span>
  <input type="submit"
    value="Subscribe">
  <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.
    </template>
  </div>
</form>

Use show-first-on-submit strategy to show the first validation message when the user submit the form.

Please enter your first and last name separated by a space (e.g. Jane Miller)
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-input-text-xhr"
  custom-validation-reporting="show-first-on-submit">
  <input type="text"
    id="show-first-on-submit-name"
    name="name"
    placeholder="Name..."
    required
    pattern="\p{L}+\s\p{L}+">
  <span visible-when-invalid="valueMissing"
    validation-for="show-first-on-submit-name"></span>
  <span visible-when-invalid="patternMismatch"
    validation-for="show-first-on-submit-name">
    Please enter your first and last name separated by a space (e.g. Jane Miller)
  </span>
  <input type="email"
    id="show-first-on-submit-email"
    name="email"
    placeholder="Email..."
    required>
  <span visible-when-invalid="valueMissing"
    validation-for="show-first-on-submit-email"></span>
  <span visible-when-invalid="typeMismatch"
    validation-for="show-first-on-submit-email"></span>
  <input type="submit"
    value="Subscribe">
  <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>

Use as-you-go strategy to show validation messages as the user is interacting with the form.

Please enter your first and last name separated by a space (e.g. Jane Miller)
Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-input-text-xhr"
  custom-validation-reporting="as-you-go">
  <input type="text"
    id="as-you-go-name"
    name="name"
    placeholder="Name..."
    required
    pattern="\p{L}+\s\p{L}+">
  <span visible-when-invalid="valueMissing"
    validation-for="as-you-go-name"></span>
  <span visible-when-invalid="patternMismatch"
    validation-for="as-you-go-name">
    Please enter your first and last name separated by a space (e.g. Jane Miller)
  </span>
  <input type="email"
    id="as-you-go-email"
    name="email"
    placeholder="Email..."
    required>
  <span visible-when-invalid="valueMissing"
    validation-for="as-you-go-email"></span>
  <span visible-when-invalid="typeMismatch"
    validation-for="as-you-go-email"></span>
  <input type="submit"
    value="Subscribe">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Reset / clear form input

You can use either an <input type="reset"> or the clear action for clearing input fields.

<form class="sample-form"
  id="formResetSample"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="text"
    placeholder="Some text...">
  <input type="submit"
    value="OK">
  <input type="reset"
    value="Reset">
  <button type="button"
    on="tap:formResetSample.clear">Clear input</button>
</form>

Hiding input fields after a successful submission

Use the amp-form-submit-success class to hide input fields after a successful submission. The following CSS rule hides all form input fields after successful form submission:

form.amp-form-submit-success > input {
  display: none
}
Success!
Error!
<form class="sample-form hide-inputs"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-input-text-xhr">
  <input type="text"
    name="name"
    placeholder="Name..."
    required>
  <input type="submit"
    value="Subscribe">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=date

amp-form supports all HTML5 form types with the exception of file and image. It's recommended to use the amp-date-picker component for input fields that should contain a date.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input name="select-date"
    type="date"
    value="2020-12-30">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=month

Use type="month" for input fields that should contain a month.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input name="select-month"
    type="month"
    value="2020-12">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=week

Use type="week" for input fields that should contain a week.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="week"
    name="week_year">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=datetime-local

Use type="datetime-local" for input fields that should contain a date and time.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input name="select-datetime"
    type="datetime-local"
    value="2020-12-30T12:34:56">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=time

Use type="time" for input fields that should contain a time.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="time"
    name="time_now">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=checkbox

Use type="checkbox" to let the user select ZERO or MORE options of a limited number of choices.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="checkbox"
    id="animal1"
    name="animal1"
    value="Cats">
  <label for="animal1">I like cats</label>
  <input type="checkbox"
    id="animal2"
    name="animal2"
    value="Dogs">
  <label for="animal2"> I like dogs </label>
  <button type="submit"
    value="OK">OK</button>
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=email

Use type="email" for input fields that should contain an e-mail address.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="email"
    name="email"
    placeholder="Email...">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=hidden

Use type="hidden" to define a field not visible to a user.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="hidden"
    name="city"
    value="London">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=number

Use type="number" for input fields that should contain a numeric value.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="number"
    name="quantity"
    min="1"
    max="5">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=radio

Use type="radio" to let a user select ONLY ONE of a limited number of choices.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="radio"
    id="cat"
    name="favourite animal"
    value="cat"
    checked>
  <label for="cat">Cat</label>
  <input type="radio"
    id="dog"
    name="favourite animal"
    value="dog">
  <label for="dog">Dog</label>
  <input type="radio"
    id="other"
    name="favourite animal"
    value="other">
  <label for="other">Other</label>
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=range

Use type="range" for input fields that should contain a value within a range.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="range"
    name="points"
    min="0"
    max="10">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=tel

Use type="tel" for input fields that should contain a telephone number.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="tel"
    name="my_tel"
    placeholder="Telephone...">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input type=url

Use type="url" for input fields that should contain a URL address.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <input type="url"
    placeholder="URL..."
    name="website">
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>

Input select

Use select element for dropdowns.

Success!
Error!
<form class="sample-form"
  method="post"
  action-xhr="https://amp.dev/documentation/examples/api/submit-form-xhr">
  <select name="cars"
    id="cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="fiat">Fiat</option>
    <option value="audi">Audi</option>
  </select>
  <label for="cars">Select a car</label>
  <input type="submit"
    value="OK">
  <div submit-success>
    Success!
  </div>
  <div submit-error>
    Error!
  </div>
</form>
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기