Search code examples
amp-html

Pass AMP state in action xhr


i am working on amp-form. in action-xhr i have to pass a amp state with url.

leadProfileId is amp state.

<form method="post" action-xhr='@currentHostUrl/api/stocks/leadProfileId/negotiate' id="submitOfferForm" target="_top"
                  on="submit-success: AMP.setState({ selectedOffer : null })">
                <input hidden name="priceOffered" [value]="selectedOffer">
                <input hidden name="mobileNumber" [value]="mobileNo">
            </form>

i have tried following approaches but it's not working.

  1. '@currentHostUrl/api/stocks/{{leadProfileId}}/negotiate'
  2. "'@currentHostUrl/api/stocks/'leadProfileId'/negotiate'"

Solution

  • Jay Gray correctly asked you about where you have the amp-state tag? It is unclear where you write data using AMP.setState({ anyData: 'anyValue'})


    Unfortunately, I don't know how to make the action-xhr attribute bind from amp-state. See this topic: https://github.com/ampproject/amphtml/issues/11222

    When we don't know how to bind something in AMP, the amp-list component usually helps us out. Solution:

    <!--
         This is the minimum valid AMP HTML document. Type away
         here and the AMP Validator will re-check your document on the fly.
    -->
    <!DOCTYPE html>
    <html ⚡>
    
    <head>
      <meta charset="utf-8" />
      <link rel="canonical" href="self.html" />
      <meta name="viewport" content="width=device-width,minimum-scale=1" />
      <style amp-boilerplate>
        body {
          -webkit-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
          -moz-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
          -ms-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
          animation: -amp-start 8s steps(1, end) 0s 1 normal both;
        }
        
        @-webkit-keyframes -amp-start {
          from {
            visibility: hidden;
          }
          to {
            visibility: visible;
          }
        }
        
        @-moz-keyframes -amp-start {
          from {
            visibility: hidden;
          }
          to {
            visibility: visible;
          }
        }
        
        @-ms-keyframes -amp-start {
          from {
            visibility: hidden;
          }
          to {
            visibility: visible;
          }
        }
        
        @-o-keyframes -amp-start {
          from {
            visibility: hidden;
          }
          to {
            visibility: visible;
          }
        }
        
        @keyframes -amp-start {
          from {
            visibility: hidden;
          }
          to {
            visibility: visible;
          }
        }
      </style>
      <noscript>
        <style amp-boilerplate>
          body {
            -webkit-animation: none;
            -moz-animation: none;
            -ms-animation: none;
            animation: none;
          }
        </style>
      </noscript>
      <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-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
      <script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
      <script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
    </head>
    
    <body>
      <amp-state id="localState">
        <script type="application/json">
          {
            "leadProfileId": "api"
          }
        </script>
      </amp-state>
    
      <h2>AMP-Form</h2>
    
      <amp-list width="auto" height="100" items="." src="amp-state:localState" single-item>
        <template type="amp-mustache">
    
          <form method="post" target="_top" action-xhr="https://amp.dev/documentation/examples/{{leadProfileId}}/verify-form-input-text-xhr/">
            <button type="submit">Submit</button>
            <div submit-success>Form send successful!</div>
            <div submit-error>Form send failed!</div>
          </form>
    
        </template>
      </amp-list>
    
    </body>
    
    </html>

    Codepen: https://codepen.io/alexandr-kazakov/pen/LYNWYzY?editors=1000