AMP
  • websites

Poll

Introduction

This is a sample template for a poll in AMP. After selecting one of the answers, you will see the percentage of chosen answers.

Setup

Import the additional AMP components. This sample uses amp-form to accept poll responses.

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

This sample uses mustache templates to format poll results

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

Poll

We use amp-form to implement a poll. To avoid multiple votes by the same user, we need to identify which users have already voted. We use a cookie called POLL_USER_ID to identify the user. Given that in Safari the default behavior is to block third-party cookies from sites that have not been previously visited, this approach may not work if the page is visited via the AMP Cache. In this case we fallback to checking for duplicate entries via the CLIENT_ID.

The CLIENT_ID is a unique identifier for the user when accessing a specific origin, for example the original page or the cached version of it. This means that the same user will generate two different values when accessing different origins. That's why we use a custom cookie to track cross origin access to the poll page.

Read more about variable substitution. Currently, HTML tables are not supported in form responses, so we'll use CSS to layout the result data.

Read more about variable substitution.

In order to submit the form as soon as the user picks an option, we will add an

on attribute to each <input>, that will submit the poll on change.

What is your favorite flightless bird?

Choose your favourite flightless bird and you will discover what other users have chosen. If you have already voted, your answer will be overwritten.

Error! Looks like something went wrong with your vote, please try to submit it again. {{error}}
<form id="sample-poll" method="post" action-xhr="https://amp.dev/documentation/examples/interactivity-dynamic-content/poll/submit-poll" target="_blank">
      <input name="clientId" type="hidden" value="CLIENT_ID(POLL_USER_ID)" data-amp-replace="CLIENT_ID">
      <h3>What is your favorite flightless bird?</h3>
      <p>Choose your favourite flightless bird and you will discover what other users have chosen.
      If you have already voted, your answer will be overwritten.</p>
      <label>Penguins <input type="radio" value="0" name="answer" on="change:sample-poll.submit"></label>
      <label>Ostriches <input type="radio" value="1" name="answer" on="change:sample-poll.submit"></label>
      <label>Kiwis <input type="radio" value="2" name="answer" on="change:sample-poll.submit"></label>
      <label>Wekas <input type="radio" value="3" name="answer" on="change:sample-poll.submit"></label>
      <div submit-success>
        <script type="text/plain" template="amp-mustache">
          <p>{{Message}}</p>
          <p>Here are the results:</p>
          <table>
            {{#PollEntryResults}}
            <tr>
              <td>
                {{Answer}}
              </td>
              <td>
                {{Percentage.length}}%
              </td>
              <td>
                {{Votes}}
              </td>
              <td>
                {{#Percentage}}<span class="one-pc-fixed"></span>{{/Percentage}}
              </td>
            </tr>
            {{/PollEntryResults}}
          </table>
        </script>
      </div>
      <div submit-error>
        Error! Looks like something went wrong with your vote, please try to submit it again. {{error}}
      </div>
</form>
자세한 설명이 필요하신가요?

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

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

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

GitHub에서 샘플 수정하기