How to use AnQiCMS form tag to dynamically hide or show form fields?

Calendar 👁️ 66

In modern website operations, user experience has become one of the key indicators of whether a website is successful or not.A well-designed form that not only effectively collects information but also greatly enhances the willingness of users to interact with the website.AnQiCMS (AnQiCMS) is an efficient and customizable enterprise-level content management system that is well-versed in this field and provides flexible template tags, allowing us to easily implement the dynamic hiding or displaying of form fields, thereby creating a more intelligent and concise user interface.

The charm of dynamic forms: Why do we need them?

Imagine a user filling out a contact form, where due to a certain selection, they need to fill in or skip a series of related information, but all fields are presented in front of them at once, which undoubtedly causes visual confusion and psychological burden.The value of dynamic forms lies in their ability to intelligently hide irrelevant fields based on the user's real-time input or selection, and only present the information needed at the moment.This not only optimizes the visual layout of the form, reduces the cognitive load of users, but also improves the efficiency and data quality of form filling.For users of AnQi CMS, this means they can provide customers with a more smooth and personalized interactive experience, whether it is for collecting inquiries, customizing product needs, or user feedback, it can achieve twice the result with half the effort.

Unveiling the AnQi CMS message form tags: Building dynamic foundations

AnQi CMS, through its powerful template tag system, especiallyguestbookThe label provides a solid foundation for building dynamic forms.When you configure the message form in the background management system, you can define various fields, including text boxes, numbers, multi-line text, radio buttons, checkboxes, and drop-down selections.guestbookThe role of the label is to output these form fields defined in the background in a structured way to the front-end page.

You will use it like this in the template file.guestbookTags:

{% guestbook fields %}
    {# 在这里循环输出每个表单字段 #}
{% endguestbook %}

here,fieldsis an array object that contains all the message form fields you have configured in the background. In the loopfieldseachitemcarries the detailed information of the field, among which the most critical properties include:

  • item.NameField display name, such as 'Your Name', 'Consultation Type'.
  • item.FieldNameField unique identifier, usually used in HTML.nameThe property is also the key for identifying and operating fields by our front-end JavaScript.
  • item.Type: The type of the field, such astext/number/radio/selectand, this is crucial for us to determine how to render and control the field.
  • item.Required: Is the field required?
  • item.Items: If the field type isradio/checkboxorselect, thenItemswill include all options.

It is through these properties that we can generate HTML elements with unique identifiers for each form field in the front-end template, laying the groundwork for subsequent dynamic operations.

Implement Dynamic Hide and Show: Frontend Magic

Due to the server-side rendering of the template tags in Anqi CMS, they are already generated with the complete HTML structure at the time of page loading.Therefore, to implement the 'dynamic' hiding or displaying of form fields, we need to rely on frontend JavaScript technology.

  1. Initial State Setting:By default, hide the fields that need to be dynamically controlled through CSS.
  2. Event Listening:Listen to the change event of the form field that acts as a "trigger" (for example, a dropdown menu or a set of radio buttons).
  3. Conditional judgment and operation:When the value of the trigger field changes, JavaScript judges according to the preset logic and accordingly displays or hides other related fields.

Now, let's implement it step by step through a specific scenario. Suppose your contact form contains a dropdown for a consultation type (selectWhen the user selects "Product Inquiry", the "Product Name" input box is displayed; when selecting "After-sales Service", the "Order Number" input box is displayed; when selecting "Other", all specific fields are hidden.

Step 1: Configure the留言表单字段in the background

In the 安企CMS后台留言表单settings, you need to create the following fields:

  • Consultation Type(FieldName: inquiry_type,Type: select,Items: 产品咨询,售后服务,其他)
  • Product Name(FieldName: product_name,Type: text)
  • Order Number(FieldName: order_id,Type: text)

Second step: Transform the front-end template file

In your template file (for exampleguestbook/index.html), you will useguestbookLabel loops to output these fields. To facilitate JavaScript positioning and control, we will add a container for each field.idAttribute, and by default, the containers of the 'Product Name' and 'Order Number' fields are hidden.

`twig

{% guestbook fields %}
    {% if item.FieldName == "inquiry_type" %}
        <div class="form-group" id="field-{{ item.FieldName }}">
            <label for="{{ item.FieldName }}">{{ item.Name }}{% if item.Required %}<span class="required">*</span>{% endif %}</label>
            <select name="{{ item.FieldName }}" id="{{ item.FieldName }}" {% if item.Required %}required{% endif %}>
                {% for val in item.Items %}
                    <option value="{{ val }}">{{ val }}</option>
                {% endfor %}
            </select>
        </div>
    {% elif item.FieldName == "product_name" %}
        <div class="form-group" id="field-{{ item.FieldName }}" style="display: none;"> {# 默认隐藏 #}
            <label for="{{ item.FieldName }}">{{ item.Name }}{% if item.Required %}<span class="required">*</span>{% endif %}</label>
            <input type="{{ item.Type }}" name="{{ item.FieldName }}" id="{{ item.FieldName }}" {% if item.Required %}required{% endif %} placeholder="{{ item.Content }}">
        </div>
    {% elif item.FieldName == "order_id" %}
        <div class="form-group" id="field-{{ item.FieldName }}" style="display: none;"> {# 默认隐藏 #}
            <label for="{{ item.FieldName }}">{{ item.Name }}{% if item.Required %}<span class="required">*</span>{% endif %}</label>
            <input type="{{ item.Type }}" name="{{ item.FieldName }}" id="{{ item.FieldName }}" {% if item.Required %}required{% endif %} placeholder="{{ item.Content }}">
        </div>
    {% else %}
        {# 处理其他普通字段,例如用户姓名、联系方式、留言内容等 #}
        <div class="form-group" id="field-{{ item.FieldName }}">
            <label for="{{ item.FieldName

Related articles

After submitting the AnQiCMS message form, will the user's IP address and submission time be recorded?

## AnQiCMS comment form: Will the IP address and submission time be recorded?——Deep Analysis of AnQiCMS Data Recording Mechanism As an experienced website operations expert, I know that the collection of every data point is related to the website's security, user experience, and even compliance.When users submit information through the comment form on the website, they often care about their privacy, and website operators need to understand which data is recorded by the system and how to effectively manage these data.Today, let's delve deeply into AnQiCMS after the form submission on the comment page

2025-11-06

How to set a unique HTML `id` attribute for each input box of the AnQiCMS comment form?

As an experienced website operations expert, I know that the core of an efficient and user-friendly website lies in the exquisite grasp of details.The comment form acts as an important bridge between users and the website, its usability directly affects user experience and even conversion rate.In web development and post-maintenance, setting a unique HTML `id` attribute for each input box in the form may seem trivial, but it actually has profound significance.It can not only significantly improve the accessibility of forms, but also greatly facilitate front-end script operations, style customization, and data validation.

2025-11-06

Can the custom field of the comment form be set to a Markdown editor type?

In website operation, the message form serves as an important bridge for user interaction with the website, and its functionality and user experience often determine the quality and positivity of user feedback.AnQiCMS (AnQiCMS) is highly praised for its flexibility and customizability in content management.However, when it comes to custom fields in the comment form, especially whether they can be set to Markdown editor type, we indeed need to delve into the existing capabilities and feasible strategies of Anqi CMS.### Secure CMS

2025-11-06

Does AnQiCMS comment form support integration with third-party data analysis tools for user behavior tracking?

## AnQi CMS Feedback Form: Third-party Integration Strategy for User Behavior Tracking As an experienced website operations expert, I fully understand the importance of user behavior tracking for optimizing website performance, improving user experience, and achieving marketing goals.Many enterprise-level content management systems (CMS) provide core content management functions while also striving to meet users' needs in data analysis.Regarding AnQiCMS (AnQi CMS) comment form, does it support integration with third-party data analysis tools for user behavior tracking? This is indeed a very practical and crucial issue.

2025-11-06

Can AnQiCMS form comments display different fields based on different user groups or login status?

## The Wisdom of AnQiCMS Comment Form: How to Display Fields Differently Based on User Groups and Login Status?As an experienced website operations expert, I fully understand the importance of user experience and refined data collection for website operations.During the process of building and managing a website, the message form acts as an important bridge for interaction between users and the website. Whether its design can intelligently respond to the user's identity directly affects the efficiency of information acquisition and the user's satisfaction.

2025-11-06

In AnQiCMS, how does the system handle if the submitted data of the message form contains sensitive words?

How does the system handle data submitted from the comment form when it contains sensitive words in AnQiCMS?As an experienced website operations expert, I am well aware of the importance of managing user-generated content (UGC) for the healthy development of the website.Comments and review sections are an important bridge for user interaction with a website, but they may also become channels for the dissemination of sensitive information.AnQiCMS (AnQiCMS) fully considered this from the beginning, its built-in powerful security mechanism, especially the sensitive word filtering function, provides a solid guarantee for website operators

2025-11-06

How to ensure good user experience and responsive design for the AnQiCMS message form on mobile devices?

In the era when mobile internet occupies a dominant position, the performance of websites on mobile devices directly affects user experience and even business results.As an experienced website operations expert, I fully understand the importance of every user touchpoint, especially functions like the feedback form that directly interact with users.AnQiCMS (AnQiCMS) has provided a solid foundation for us to build a mobile-friendly message form with its efficient and flexible architecture.Today, let's delve into how to skillfully use the AnQiCMS features

2025-11-06

What filtering and search features does the AnQiCMS form submission backend management interface support?

As an experienced website operations expert, I know that the usability and functionality of a content management system's backend interface have a decisive impact on daily operational efficiency.AnQiCMS (AnQiCMS) leverages the high-performance architecture of the Go language and flexible functional design, providing strong support in many aspects for operators.

2025-11-06