使用 AJAX 提交表单时,成功变量在 Statamic Antlers 模板中不起作用

Success variable not working in Statamic Antlers template when using AJAX to submit form

提问人:Nicholas Norton 提问时间:9/26/2023 更新时间:11/2/2023 访问量:47

问:

我正在使用带有 AJAX 的 Statamic Form 和 Antlers 模板来提交表单。但是,提交表单时,成功变量不再起作用,因此我无法更新文本以显示表单已提交。

{{ form:property_valuation id="propertyValuationForm" handle="property_valuation" x-data="formSetup()" x-on:submit.prevent="getResults($el)" }}
            {{ if success }}
              <div>
                  <p class="fw-bold text-white">
                    Thanks! We have received your request. We will reach out to you soon.
                  </p>
              </div>
            {{ else }}
              {{ if errors }}
                <div class="bg-danger text-white p-2">
                  {{ errors }}
                    {{ value }}<br>
                  {{ /errors }}
                </div>
              {{ /if }}

              <div class="row row-cols-md-1 row-cols-lg-2 align-items-center gy-2">
                <div>
                  <label for="field-email" class="visually-hidden">Email Address</label>
                  <input 
                    type="email"
                    id="field-email"
                    name="email"
                    class="form-control text-black-50-placeholder py-2"
                    placeholder="Your email address"
                  >
                </div>
    
                <div>
                  {{ partial:components/btn type="submit" isButton="true" isFullwidth="true" wrapperClass="has-light-line" }}
                    {{ form_valuation:button_text }}
                  {{ /partial:components/btn }}
                </div>
              </div>
            {{ /if }}
          {{ /form:property_valuation }}
          <script>
            function formSetup() {
              return {
                formResult: false,
                getResults(formElement) {
                  let context = this;
                  let data = new FormData(formElement);
                  let xhr = new XMLHttpRequest();
                  xhr.open(formElement.method, formElement.action);
                  xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                  xhr.send(data);
                  xhr.onload = function() {
                    if (xhr.readyState === xhr.DONE) {
                      context.formResult = JSON.parse(xhr.response);
                      }
                    }
                  };
                }
              };
            }
          </script>

这个{{ if success }}在这里不起作用,有没有办法访问它,就像我没有使用AJAX一样?或者是否有另一种方法可以更新 for 的内容以仅显示成功文本?

Ajax 形成 statamic

评论


答: 暂无答案