Message Form Tag

Description: Used to obtain the backend settings for the message form

Usage:{% guestbook 变量名称 %}If the variable is defined as fields{% guestbook fields %}...{% endguestbook %}

The supported parameters of guestbook are

  • Site IDsiteId siteIdGenerally, it is not necessary to fill in. If you have created multiple sites using the multi-site management on the backend and want to call data from other sites, you can do so by specifyingsiteIdTo implement the call to data from a specified site.

fields is an array object, so you need to useforin a loop to output

item is the variable within the for loop, the available fields are:

  • Form nameName
  • Form variableFieldName
  • Form typeTypeThere are 6 possible values for form type: text typetext、Number typenumber、Multi-line text typetextarea、Single choice typeradio、Multiple choice typecheckbox、Dropdown selection typeselect.
  • Is RequiredRequiredRequired When the value is true, it indicates that it is required. When the value is false, it indicates that it is not required.
  • Default value of the formContent
  • Split into array default valueItemsWhen the form type is Single selection typeradio、Multiple choice typecheckbox、Dropdown selection typeselectWhen, each option of their choices constitutes an Items array, which can be output by a for loop.

Submit message form

The form submission requires using the 'form' form submission, and the receiving address of the background is:/guestbook.htmlThe fields that need to be submitted include:

Field Is Required Description
user_name Yes The username of the user who leaves a message
contact Yes Contact information, such as mobile phone, landline, WeChat, QQ, etc.
content Yes Message content
Other custom fields Determined according to the settings Fields set for additional form fields added in the background, based on whether they are required according to the settings
return No Specify the format of the backend return after submission, optional values include:html/jsonDefault is html

Code example

The following code can be used to loop and output all the fields set.

<form method="post" action="/guestbook.html">
{% guestbook fields %}
    {% for item in fields %}
    <div>
        <label>{{item.Name}}</label>
        <div>
            {% if item.Type == "text" || item.Type == "number" %}
            <input type="{{item.Type}}" name="{{item.FieldName}}" {% if item.Required %}required lay-verify="required"{% endif %} placeholder="{{item.Content}}" autocomplete="off">
            {% elif item.Type == "textarea" %}
            <textarea name="{{item.FieldName}}" {% if item.Required %}required lay-verify="required"{% endif %} placeholder="{{item.Content}}" rows="5"></textarea>
            {% elif item.Type == "radio" %}
            {%- for val in item.Items %}
            <input type="{{item.Type}}" name="{{item.FieldName}}" value="{{val}}" title="{{val}}">
            {%- endfor %}
            {% elif item.Type == "checkbox" %}
            {%- for val in item.Items %}
            <input type="{{item.Type}}" name="{{item.FieldName}}[]" value="{{val}}" title="{{val}}">
            {%- endfor %}
            {% elif item.Type == "select" %}
            <select name="{{item.FieldName}}">
                {%- for val in item.Items %}
                <option value="{{val}}">{{val}}</option>
                {%- endfor %}
            </select>
            {% endif %}
        </div>
    </div>
    {% endfor %}
    <div>
        <div>
            <button type="submit">提交留言</button>
            <button type="reset">重置</button>
        </div>
    </div>
{% endguestbook %}
</form>

If you want to customize the form display, you can also use the conventional input to organize the display, such as:

<form method="post" action="/guestbook.html">
  <input type="hidden" name="return" value="html">
  <div>
    <label>用户名</label>
    <div>
      <input type="text" name="user_name" required lay-verify="required" placeholder="请填写您的昵称" autocomplete="off">
    </div>
  </div>
  <div>
    <label>联系方式</label>
    <div>
      <input type="text" name="contact" required lay-verify="required" placeholder="请填写您的手机号或微信" autocomplete="off">
    </div>
  </div>
  <div>
    <label>留言内容内容</label>
    <div>
      <textarea name="content" placeholder="" id="comment-content-field" rows="5"></textarea>
    </div>
  </div>
    <div>
        <div>
            <button type="submit">提交留言</button>
            <button type="reset">重置</button>
        </div>
    </div>
</form>