在基本的Shopify联系表单上出现错误后保留表单数据

use*_*295 0 form-data shopify contact-form form-fields

我已添加了Shopify文档中描述的基本联系表单.

我的代码:

{% form 'contact' %}

{% if form.posted_successfully? %}
<div class="successForm feedback">
  <p>Thanks for contacting us. We'll get back to you as soon as possible.</p>
</div>
{% endif %}

{% if form.errors %}
<div class="errorForm feedback">
  <p>Sorry, we were unable to submit your inquiry because it contained {{ form.errors | size | pluralize: 'an error', 'a few errors' }}.<br/>Please correct the following and submit again:</p>
  {% for field in form.errors %}
  <p><strong>The {{ field | capitalize | replace: 'Body', 'Comments / questions' }} field {{ form.errors.messages[field] }}.</strong></p>
  {% endfor %}
</div>
{% endif %}

<div id="contactFormWrapper">
  <h3>Personal details</h3>
  <ul>
    <li>
      <label>First name:</label>
      <input type="text" id="contactFormFirstName" name="contact[first-name]" placeholder="" />
    </li>
    <li>
      <label>Last name:</label>
      <input type="text" id="contactFormLastName" name="contact[last-name]" placeholder="" />
    </li>          
    <li>
      <label>Email address:</label>
      <input type="email" id="contactFormEmail" name="contact[email]" placeholder="" />
    </li>
    <li>
      <label>Telephone number:</label>
      <input type="telephone" id="contactFormTelephone" name="contact[phone]" placeholder="" />
    </li>
    <li>
      <label>City:</label>
      <input type="text" id="contactFormCity" name="contact[city]" placeholder="" />
    </li>
    <li>
      <label>Country:</label>
      <input type="text" id="contactFormCountry" name="contact[country]" placeholder="" />
    </li>
  </ul>
  <h3>Items</h3>
  <ul>
    <li>
      <label>Items:</label>
      <input type="text" id="contactFormItems" name="contact[items]" placeholder="" />

    </li>
    <li>
      <label>Questions:</label>
      <textarea rows="10" id="contactFormComments" name="contact[body]" placeholder=""></textarea>
    </li>
  </ul>
  <div class="clearfix"></div>
  <div class="main-button-container">
    <input type="submit" id="contactFormSubmit" value="Submit Enquiry" class="btn button-style" />
  </div>
</div>

{% endform %}
Run Code Online (Sandbox Code Playgroud)

但是,如果用户尝试提交表单但填写错误,则页面将刷新并显示错误消息并清除所有数据.从用户体验的角度来看,这并不理想,因为用户必须重新输入所有内容.

如何在表单错误后保留所有以前的数据?请帮忙.

hjb*_*lok 6

您可以使用该value属性显示提交的数据.例如:

<input type="text" id="contactFormEmail" name="contact[email]" value="{{form.email}}" />
Run Code Online (Sandbox Code Playgroud)

或者textarea:

<textarea id="contactFormComments" name="contact[body]">{{form.body}}</textarea>
Run Code Online (Sandbox Code Playgroud)