How to retrieve and display custom form fields in AnQiCMS comment form?

Calendar 👁️ 60

When using AnQiCMS to manage website content, the online message function is undoubtedly an important way to interact with visitors, collect feedback, or gather information about potential customers.In addition to the basic name, contact information and message content, we often need to collect more specific and personalized information, such as 'Your project budget', 'The type of service you are interested in', or 'The expected contact time' and so on.AnQi CMS provides flexible custom message field functionality, allowing us to easily meet these needs.How can I get and elegantly display these custom form fields on the website front page?

This usually involves modifying template files, by using the template tags built into Anqí CMS, we can display the custom fields defined in the background to the users. The core is to understandguestbookThe principle of the tag and the data structure it returns.

UtilizeguestbookTag to get custom field

To display custom message form fields, you first need to add them to your message page template (usuallyguestbook/index.htmlOr use the message template file you customizedguestbookThe tag will retrieve all the custom form fields you configure in the "Function Management" -> "Website Messages" section of the Anqi CMS backend.

The label usage is very concise:

{% guestbook fields %}
    {# 你的表单字段渲染逻辑将在这里 #}
{% endguestbook %}

Here, fieldsis composed byguestbookThe variable created by the label is an array containing information about all custom form fields. Next, we can iterate over thisfieldsAn array that dynamically renders each form field.

Understand the data structure of form fields and render them dynamically

fieldsEach element in the array represents a custom field, which we callitem. EachitemIt includes some key properties that help us decide how to render it:

  • item.Name: This is the display name of the field, which is the label text that users see on the form, such as “Your Name”, “Message Subject”, etc.
  • item.FieldNameThis is the unique identifier for the form field, which will be used as the HTML input box'snameattribute value, and the backend will receive the data according to this name when the form is submitted.
  • item.TypeThis is the field type, which determines which HTML input element it should render, for exampletext(Single-line text),number(Number),textarea(Multi-line text),radio(Single selection),checkbox(Select multiple) orselect(Dropdown select).
  • item.Required: A boolean value indicating whether the field is required.
  • item.ContentFortext/number/textareaThe field type, this is usually their default value or placeholder text.
  • item.ItemsForradio/checkbox/selectThe type field, this is an array that includes all available option values.

With this information, we can work with oneforiterate over, based onitem.Typedifferences, usingiforelifA statement to generate the corresponding HTML input box.

Here is a commonly used code example that shows how to traversefieldsVariables and dynamically render different types of custom fields, while also including the default required Anqi CMS message formuser_name/contactandcontentField:

<form method="post" action="/guestbook.html">
    <input type="hidden" name="return" value="html"> {# 可选:指定后台返回HTML或JSON #}

    {# 安企CMS留言表单默认字段 #}
    <div>
        <label for="user_name">您的称呼</label>
        <input type="text" id="user_name" name="user_name" required placeholder="请填写您的称呼" autocomplete="off">
    </div>
    <div>
        <label for="contact">联系方式</label>
        <input type="text" id="contact" name="contact" required placeholder="请填写您的手机号或微信" autocomplete="off">
    </div>
    <div>
        <label for="content">留言内容</label>
        <textarea id="content" name="content" required placeholder="请填写您的留言内容" rows="5"></textarea>
    </div>

    {# 动态渲染自定义字段 #}
    {% guestbook fields %}
        {% for item in fields %}
            <div>
                <label for="{{item.FieldName}}">{{item.Name}}</label>
                {% if item.Type == "text" or item.Type == "number" %}
                    <input type="{{item.Type}}" id="{{item.FieldName}}" name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" autocomplete="off">
                {% elif item.Type == "textarea" %}
                    <textarea id="{{item.FieldName}}" name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" rows="3"></textarea>
                {% elif item.Type == "radio" %}
                    {% for val in item.Items %}
                        <label>
                            <input type="{{item.Type}}" name="{{item.FieldName}}" value="{{val}}" {% if item.Required and loop.first %}required{% endif %}> {{val}}
                        </label>
                    {% endfor %}
                {% elif item.Type == "checkbox" %}
                    {% for val in item.Items %}
                        <label>
                            <input type="{{item.Type}}" name="{{item.FieldName}}[]" value="{{val}}" {% if item.Required and loop.first %}required{% endif %}> {{val}}
                        </label>
                    {% endfor %}
                {% elif item.Type == "select" %}
                    <select id="{{item.FieldName}}" name="{{item.FieldName}}" {% if item.Required %}required{% endif %}>
                        {% for val in item.Items %}
                            <option value="{{val}}">{{val}}</option>
                        {% endfor %}
                    </select>
                {% endif %}
            </div>
        {% endfor %}
    {% endguestbook %}

    <div>
        <button type="submit">提交留言</button>
        <button type="reset">重置</button>
    </div>
</form>

In the code above, we first hard-coded the fields that AnQi CMS will handle by defaultuser_name/contactandcontentThen, through the{% guestbook fields %}tag to get the custom fields, and then through aforLoop throughfieldsAn array. Within the loop, we utilizeif/elifStructure based onitem.Typevalues to render different types of form elements. For example,textandnumbertype usage<input type="text/number">,textareatype usage<textarea>.radio/checkboxandselecttype, we will further traverseitem.Itemsarrays to generate their respective options.

It should be noted that,checkboxType ofname属性通常需要加上[]suffixes likename="FieldName[]"),so that multiple selected values can be received when submitting the form. In addition,item.RequiredThe attribute is used to add HTML5'srequiredattribute to implement simple client-side validation.

Form submission注意事项

The message form'sactionattribute should point to/guestbook.htmlandmethodMust bepost. In addition to custom fields, make sure that the page includesuser_name/contactandcontentThese are the default message fields of the Anqi CMS, even if you want them to be hidden or pre-filled in some other way.If these core fields are missing, the message may not be submitted successfully.You can also add a hidden field<input type="hidden" name="return" value="html">Control whether the backend returns an HTML page or JSON data after processing the message.

By using the above method, you can flexibly display and collect various customized message information on the front-end page according to the background configuration, greatly enhancing the functionality and data collection ability of the website's interaction with visitors.


Frequently Asked Questions (FAQ)

  1. Why is the custom message field I configured on the AnQi CMS backend not displayed on the front-end page?This is usually because you haven't added or used the website comment template file correctly{% guestbook fields %}Tags to dynamically render these fields. The custom field configuration in the Anqi CMS backend only defines the structure and properties of the fields, but does not automatically insert them into the front-end page.You need to manually edit the template file corresponding to the message page (for exampletemplate/你的模板名/guestbook/index.html),and use the sample code in the article,toguestbookLoop through and render these fields with the tag.

  2. After submitting my comment form, the data of the custom fields is not saved, or the data saved to the background is incorrect, how should I investigate?First, make sure your formactionproperties point to/guestbook.htmlandmethodWithpostNext, check the HTML of each custom fieldinput/textareaorselectlabel'snameThe attribute value should match the custom field settings in the Anqi CMS backendFieldNameentirely (including case). For multi-select fields (checkboxEnsure thatnameProperties used[]Suffix (for example,name="your_custom_field_name[]"In addition, also confirmuser_name/contactandcontentThese three default fields required by Anqi CMS have been submitted correctly.

  3. How can I display only a few custom fields in the background instead of all?Of course you can. The code provided in the article is a universal example of dynamically rendering all fields. If you only want to display a few specific custom fields, you can choose not to useforLoop throughfieldsArray, but is directly defined according to the backenditem.FieldNameto hard-code the fields you need. For example, if you have a项目预算custom field, itsFieldNameWithproject_budget, you can directly write it in the template like this:

    {% guestbook fields %} {# 仍然需要此标签来获取所有字段信息 #}
        {# 假设你知道 project_budget 字段在 fields 数组中的位置或其 FieldName #}
        {# **做法是过滤 fields 数组或者直接使用已知 FieldName 查找 #}
        {% set project_budget_field = null %}
        {% for item in fields %}
            {% if item.FieldName == "project_budget" %}
                {% set project_budget_field = item %}
                {% break %} {# 找到后退出循环 #}
            {% endif %}
        {% endfor %}
    
        {% if project_budget_field %}
            <div>
                <label for="{{project_budget_field.FieldName}}">{{project_budget_field.Name}}</label>
                <input type="{{project_budget_field.Type}}" id="{{project_budget_field.FieldName}}" name="{{project_budget_field.FieldName}}" {% if project_budget_field.Required %}required{% endif %} placeholder="{{project_budget_field.Content}}">
            </div>
        {% endif %}
    {% endguestbook %}
    

    This method is a bit more complex, but it provides the greatest flexibility, allowing you to only display or customize the rendering of specific fields.

Related articles

How to display user comment lists and implement pagination in AnQiCMS?

In website content operation, user comments are an important part of enhancing interactivity and activity, and reasonable pagination can ensure that the comment list is both complete and loads smoothly.AnQiCMS (AnQiCMS) provides powerful and flexible template tags, allowing you to easily display and manage user comments on the front-end page, and implement pagination functionality for the comment list.

2025-11-08

How to use AnQiCMS tags to display the document list under a specific Tag?

In AnQiCMS, tags are a powerful content organization method that helps us associate content with different categories and models more flexibly.Using labels appropriately can not only optimize the SEO performance of a website, but also significantly improve the user's experience in discovering content on the website.When we want to focus on displaying all relevant documents under a specific tag, AnQiCMS provides a set of intuitive and efficient template tags to meet this need.This article will introduce in detail how to use the built-in template tags of AnQiCMS

2025-11-08

How to display the list of related documents in AnQiCMS, what is the logic?

How to effectively guide users to discover more interesting content in a content management system is the key to improving user experience and website depth.AnQiCMS handles the 'related document list' feature by providing a flexible and intelligent mechanism that allows us to easily display other articles or products closely related to the current content on the website. This not only helps to extend the user's stay on the website but also optimizes the search engine crawling efficiency through internal links.### AnQiCMS

2025-11-08

How to display custom field content on the AnQiCMS document detail page?

When managing website content in AnQiCMS, we often encounter the need to add some unique information for different types of documents (such as articles, products, etc.).AnQi CMS provides flexible content model features, allowing us to customize fields to meet these personalized needs.How can these custom fields be displayed on the document detail page of the website after they are created and filled with data? This is actually simpler than you imagine, AnQiCMS's powerful template tag system provides us with various convenient ways to achieve this.--- ###

2025-11-08

How to display pagination links in AnQiCMS and control the number of page numbers displayed?

In website operation, effectively managing and displaying a large amount of content is the key to improving user experience.When the content list is too long, a reasonable pagination mechanism can not only make it easier for users to browse, but also optimize the page loading speed, and indirectly improve the search engine friendliness.In AnQiCMS, implementing pagination and flexibly controlling the display quantity of page numbers is a relatively direct and powerful process.--- ## Display pagination links elegantly and flexibly control the number of pages in AnQiCMS In your AnQiCMS website, when the number of articles, products, or other list content increases

2025-11-08

How to set and display the global information of AnQiCMS, such as Logo, filing number, and copyright?

AnQiCMS provides an intuitive and powerful way to manage and display the global information of the website, such as the website logo, filing number, and copyright statement.This information is not only an important part of the website's brand image, but also concerns the legal and compliant operation of the website.The detailed introduction on how to set and utilize these global information will follow. ### Set global information in the background All the global information of the website is concentrated in the "Global Function Settings" of AnQiCMS backend.

2025-11-08

How to implement conditional logic in AnQiCMS to control the display of content?

In website operation, displaying different content based on different conditions is the key to improving user experience and achieving refined operation.AnQiCMS provides powerful and flexible template tags and logical judgment functions, allowing us to easily implement conditional display of content, thereby meeting various business needs.This article will delve into how to make use of the template engine features in AnQiCMS, controlling the display of website content precisely through conditional logic judgments.

2025-11-08

How does the AnQiCMS template loop through arrays or objects to display list data?

In the Anqi CMS template system, the core of looping through arrays or objects and displaying list data lies in understanding its Django-like template syntax, especially the use of the `for` loop tag.This mechanism makes the presentation of dynamic content intuitive and efficient, whether it is for article lists, category navigation, or product displays, it can adapt flexibly. ### Core Mechanism: The Use of `for` Loops Data traversal in Anqi CMS templates mainly relies on `{% for ...in ...

2025-11-08