How to dynamically build and display custom form fields for leaving messages in the template?

Calendar 👁️ 80

In website operation, the message form is an important bridge for users to interact with the website, collect user feedback, or potential customer information.A flexible and customizable comment form can greatly enhance the practicality and user experience of a website.AnQiCMS (AnQi CMS) provides strong support in this regard, allowing users to customize the fields of the message form in the background, and dynamically build and display these fields in the front-end template to meet various personalized business needs.

AnQiCMS flexibility of the comment form

One of AnQiCMS's core strengths lies in its 'flexible content model' design, which also extends to the website's comment feature.The system not only provides basic message functionality, but also allows users to freely add and edit the fields of the message form according to specific business scenarios.This means that whether you need to collect users' contact information, product requirements, service feedback, or any other customized information, it can all be achieved through simple configuration without the need for complex code changes.This high degree of customization makes AnQiCMS the ideal tool for small and medium-sized enterprises and content operation teams to interact with users and collect data.

Configure custom message field in the background

In the AnQiCMS backend management interface, you can usually find the related settings for 'Website Message Management' under the 'Function Management' module.Here, you will find an intuitive interface for defining the various fields of the comment form.You can set each field:

  • Form name (Name)This is the name of the field displayed to the user on the front end, for example, "Your name", "Phone number", etc.
  • Form Variable (FieldName)This is the key name corresponding to the field when submitting the form, it is usually recommended to use lowercase English letters for easy program processing.
  • Field Type (Type): AnQiCMS provides various field types to meet different data collection needs, including:
    • Single-line Text (text)Used to collect brief text information, such as name, email.
    • Number (number)Used to collect numbers, such as age, quantity.
    • Multiline text (textarea): Used to collect longer text information, such as detailed descriptions, comments.
    • Single choice (radio): Provides a set of predefined options, from which the user can only choose one.
    • Multiple choice (checkbox)Provide a set of preset options, the user can select multiple options.
    • Dropdown selection (select)Provide a dropdown menu, the user selects an option from the preset options.
  • RequiredCan mark a field as required to ensure that users submit necessary information.
  • Default values or options (Content / Items): For single-line text, numbers, and multi-line text, you can set default placeholder text; for radio buttons, checkboxes, and dropdown selections, this is where you define specific options.

Configure these settings to create highly personalized, business-specific comment forms.

Dynamically build and display comment form fields in the template.

How can these fields be dynamically displayed on the website front-end after the message field is configured in the background? AnQiCMS providesguestbookLabel, this label is the key to connect the backend configuration with the frontend display.

{% guestbook fields %}The label is used to retrieve all the form field information set in the backend and store it in a namedfieldsin an array object. You can traverse thisfieldsarray, and dynamically generate the corresponding HTML form elements based on the type of each field.

Here is a typical template code example that shows how to traversefieldsthe array to generate different form elements:

<form method="post" action="/guestbook.html">
{% guestbook fields %}
    {% for item in fields %}
    <div>
        <label>{{item.Name}}</label>
        <div>
            {% if item.Type == "text" or item.Type == "number" %}
            <input type="{{item.Type}}" name="{{item.FieldName}}" {% if item.Required %}required{% endif %} placeholder="{{item.Content}}" autocomplete="off">
            {% elif item.Type == "textarea" %}
            <textarea name="{{item.FieldName}}" {% if item.Required %}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}}"> {{val}}
            {%- endfor %}
            {% elif item.Type == "checkbox" %}
            {%- for val in item.Items %}
            <input type="{{item.Type}}" name="{{item.FieldName}}[]" value="{{val}}" title="{{val}}"> {{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>

Code analysis:

  1. <form method="post" action="/guestbook.html">: This is a standard HTML form declaration,actionproperty points to/guestbook.htmlThis is the default interface address for AnQiCMS to handle message submission.
  2. {% guestbook fields %}Calling AnQiCMS'sguestbooklabel, assigns the field information configured in the background tofieldsVariable.
  3. {% for item in fields %}: Loop through.fieldsEach field object in the array.itemThe variable represents the field being processed.
  4. {{item.Name}}: Shows the field name, such as "Name".
  5. {% if item.Type == "text" or item.Type == "number" %}According toitem.TypeThe value determines the field type.
    • If it is a "text" or "number", a one is generated<input>tag, typeThe attribute is dynamically set toitem.Type,nameset the attribute toitem.FieldName(Very important, used for backend data reception),placeholderis set toitem.Content.{% if item.Required %}required{% endif %}Then add HTML5'srequiredProperty.
    • {% elif item.Type == "textarea" %}: Generate<textarea>multi-line text box.
    • {% elif item.Type == "radio" %}: loopitem.Items(option array), generate a tag for each optiontype="radio"of<input>label. Note that all radio buttons'nameProperties are the same to achieve a radio button effect.
    • {% elif item.Type == "checkbox" %}: Similarly, loopitem.Items, generate one for each optiontype="checkbox"of<input>Label. The properties of the checkbox.nameare usually accompanied by[]This indicates that multiple values can be submitted.
    • {% elif item.Type == "select" %}: Generate<select>Loop through the dropdown menu.item.ItemsGenerate for each option.<option>.
  6. item.FieldNameThis variable name corresponds exactly to the form variable you set in the background, the AnQiCMS backend will identify and receive the data submitted by the user through thisname.
  7. required: HTML5 form validation attribute, if

Related articles

How to obtain the contact information, phone number, email, social media, and other information configured in the background "Contact Settings"?

AnQiCMS provides a convenient function for website administrators to centrally manage and display contact information.The contact information, phone number, email, social media and other information configured in the "Contact Settings" backstage can be flexibly displayed on the website front end through simple template tags, greatly improving the efficiency and consistency of content updates. ### Understand the "Contact Information Settings" on the backend First, let's get familiar with the location of this information in the AnQiCMS backend configuration.

2025-11-08

How to get and display the system parameters configured in the global settings of the AnQiCMS template?

In the AnQiCMS template, efficiently obtaining and displaying the system parameters configured in the background "Global Settings" is the key to ensuring unified website information and convenient management.AnQiCMS provides a set of intuitive and powerful template tags, allowing developers and operators to easily achieve this goal without delving into complex programming code.

2025-11-08

How to use the `languages` tag to build a language switch menu and output the `hreflang` tag in a multilingual site?

Building multilingual sites in AnQiCMS (AnQiCMS) can not only expand your market range but also provide a more user-friendly access experience for users in different regions.To achieve this goal, the core lies in effectively utilizing the `languages` tag to create a language switch menu and correctly output the `hreflang` tag to optimize search engine recognition.AnQiCMS is a content management system that focuses on enterprise applications, with its powerful multilingual support being one of its highlights.It allows you to create multiple language versions for website content

2025-11-08

How to dynamically output the SEO title, keywords, and description (TDK) on AnQiCMS templates?

The performance of a website in search engines directly affects the efficiency of content access, and the title (Title), keywords (Keywords), and description (Description), abbreviated as TDK, are the 'business card' for search engines to understand page content.Effectively manage and dynamically output TDK, which can significantly improve the SEO effect and click-through rate of the website.AnQiCMS is a content management system that highly values SEO and provides flexible and powerful features, making it easy to dynamically configure page TDK.

2025-11-08

How to integrate captcha functionality in comment or message form to prevent spam?

Websites and article comments are an important bridge for interaction between websites and users, and they can effectively improve the activity and user stickiness of the content.However, these open interactive areas are often targeted by spammers, and a large amount of meaningless or malicious information not only damages the image of the website but may also affect search engine rankings.To effectively curb such problems, integrating a captcha function into the message or comment form is an effective method.A safe CMS as an efficient website building tool, fully considering common challenges in content operation and providing convenient solutions

2025-11-08

How to customize and output Json-LD structured data in AnQiCMS template to optimize search engine display?

In today's digital marketing environment, the importance of Search Engine Optimization (SEO) is self-evident, and structured data is one of the key factors for improving a website's performance on Search Engine Results Pages (SERP).AnQiCMS is a system focused on providing efficient content management solutions, which offers powerful SEO optimization tools to website operators with its flexible customization and output of Json-LD structured data in templates.

2025-11-08

How to retrieve and display detailed information about website users or user groups (such as VIP level, balance)?

In Anqi CMS, implementing the acquisition and display of detailed information of website users or user groups is a key link in building personalized member systems and enriching user experience.Whether it is to display the user's VIP level, account balance, or the name of the user group they belong to, AnQiCMS provides intuitive and powerful template tags to help us easily achieve these functions. ### Deep Understanding of AnQiCMS User and User Group System AnQiCMS is built with a complete user management and user group (VIP system) function.

2025-11-08

How to customize the display layout of the article detail page in AnQi CMS?

When building a website with AnQiCMS, we often need to give different types of articles or specific articles a unique appearance and layout to better display the content and enhance the user experience.AnQiCMS provides a flexible mechanism that allows users to customize the display layout of article detail pages according to their needs, whether it is for the entire content model, a specific category, or a single independent article.Understand and make good use of these customization capabilities, it is the key to leveraging the advantages of AnQiCMS content management.The detailed steps to implement the personalized layout of the article detail page will be introduced below

2025-11-08