How to receive and display custom form fields from the backend in a frontend comment form?

Calendar 👁️ 55

On website operation, the message form is an important channel for user interaction, collecting feedback and leads.AnQiCMS provides a flexible message function, one very practical feature of which is to allow us to customize form fields in the background and seamlessly present them in the front-end message form to meet the needs of personalized information collection in different business scenarios.

Next, we will discuss in detail how to implement this feature in AnQi CMS.

1. Define custom留言fields in the back-end

Firstly, we need to enter the Anqi CMS backend management interface and set up the custom fields required for the message form.

  1. Navigate to the message form management:After logging in to the backend, please find and click on the "Function Management" in the left menu, and then select "Website Message" in the dropdown menu.This is the centralized location for all settings related to website comments.
  2. Create or edit custom fields:On the "website message" page, you will see an area named "custom message field". Click to enter and you can add new fields or edit existing fields.
    • Parameter name (Name):This is the field name displayed in the background interface for administrators, for example, "Your title," "Industry," "Consultation type," etc., it is recommended to use intuitive Chinese descriptions.
    • Field Name (FieldName):This is the unique variable name used to identify this field in the front-end template, and it is also the key name corresponding to the data submitted in the form. Please make sure to use English lowercase letters and ensure its uniqueness, for examplehonorific/industry/inquiry_type.
    • Field Type (Type):AnQi CMS provides various field types to meet different input requirements, including:
      • text(Single-line text): Used for short text input, such as name, phone number.
      • number(Number): Only numbers are allowed, such as quantity.
      • textarea(Multiline text): Used for long text input, such as detailed message content.
      • radio(Single selection): Provide multiple options for users to select.
      • checkbox(Multiple choice): Provide multiple options for user selection.
      • select(Dropdown selection): Provide a dropdown menu for user selection.
    • (Required):Check this box to make this field required in the front-end form.
    • Default value (Content):For single-line text and multi-line text, you can set the placeholder here.For single-choice, multiple-choice, and dropdown selection types, this is the key content defining the options.Each option should be entered on a separate line, and the system will automatically parse it as a separate option.

After defining the field, save the settings. These custom fields are ready for the dynamic generation of the front-end form.

II. Dynamically display custom fields in the front-end template.

After defining the background fields, the next step is to use the Anqi CMS template tags in the front-end message form template file to dynamically retrieve and render these fields.

  1. Find the comment form template:通常,留言表单位于模板目录下的guestbook/index.htmlor in a file with a similar name.

  2. UseguestbookLabel field acquisition:Anqi CMS provides a special field for obtaining the comment form.guestbookLabel. You can use it in the form of<body>to iterate over all custom fields:

    <form method="post" action="/guestbook.html">
        {# 默认必填字段,您可能需要根据模板实际情况调整 #}
        <div>
            <label>您的姓名</label>
            <input type="text" name="user_name" required placeholder="请填写您的姓名">
        </div>
        <div>
            <label>联系方式</label>
            <input type="text" name="contact" required placeholder="请填写您的联系方式">
        </div>
        <div>
            <label>留言内容</label>
            <textarea name="content" required rows="5" placeholder="请填写您的留言内容"></textarea>
        </div>
    
        {# 动态加载后台自定义字段 #}
        {% 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="3"></textarea>
                    {% elif item.Type == "radio" %}
                        {%- for val in item.Items %}
                        <label><input type="radio" name="{{ item.FieldName }}" value="{{ val }}" {% if item.Required %}required{% endif %}> {{ val }}</label>
                        {%- endfor %}
                    {% elif item.Type == "checkbox" %}
                        {%- for val in item.Items %}
                        <label><input type="checkbox" name="{{ item.FieldName }}[]" value="{{ val }}" {% if item.Required %}required{% endif %}> {{ val }}</label>
                        {%- endfor %}
                    {% elif item.Type == "select" %}
                    <select name="{{ item.FieldName }}" {% if item.Required %}required{% endif %}>
                        <option value="">请选择</option>
                        {%- for val in item.Items %}
                        <option value="{{ val }}">{{ val }}</option>
                        {%- endfor %}
                    </select>
                    {% endif %}
                </div>
            </div>
            {% endfor %}
        {% endguestbook %}
    
        <div>
            <button type="submit">提交留言</button>
            <button type="reset">重置</button>
        </div>
    </form>
    
  3. Parse code example:

    • {% guestbook fields %}This tag will retrieve all the custom message fields defined in the background and assign them as an array to a variablefields.
    • {% for item in fields %}: We go throughforLoop throughfieldsEach field in the arrayitem.
    • {{ item.Name }}: Displays the field name, which is set as the 'parameter name' in the background.
    • name="{{ item.FieldName }}"This is the most important attribute of the form element, its value is the 'call field' set by the background, ensuring that the data can be correctly associated with the background fields when submitted. Note the checkboxcheckboxofnameNeed to add[]such asname="{{ item.FieldName }}[]"In order to receive multiple values by the backend.
    • type="{{ item.Type }}": The front-end is dynamically set according to the field type defined by the backend.inputlabel'stypeProperty.
    • {% if item.Required %}required{% endif %}: If the backend is set as required, HTML5 will be added here.requiredProperty, the browser will perform basic non-empty validation.
    • placeholder="{{ item.Content }}": Set the default value defined on the backend as the placeholder text for the input box.
    • {% for val in item.Items %}: For radio, checkbox, and dropdown selection types,item.Itemsis an array containing all the option values, we can loop through it again to generateradio/checkboxoroptionelements.

By following these steps, you can achieve the flexibility of receiving and displaying custom fields from the backend in the Anqi CMS frontend comment form.In this way, no matter how your business needs change, you can easily adjust the comment form to collect the required information, greatly enhancing the interactivity and data collection efficiency of the website.

Frequently Asked Questions (FAQ)

1. Do I have to manually write the HTML code for each custom field in the template?

No, as demonstrated in the article, you can useguestbooktemplate tags andforLoop to dynamically traverse and generate HTML code for all custom fields.This way, no matter how many fields you add or delete in the background, the front-end form will automatically update without manually modifying the template file, greatly improving maintenance efficiency.

2. How does the front-end obtain and display options for custom fields that are selection types (single-choice, multiple-choice, dropdown)?

When you are in the back-end for selection types (radio/checkbox/selectWhen defining field options, each option should be entered on a separate line. In the frontend template, through theguestbookTags retrieved from comments.itemThe object will include aitem.Itemsproperty, it is an array containing all the option values. You can use it againforLoop throughitem.ItemsGenerate these options dynamically(<input type="radio">/<input type="checkbox">or<option>tags).

How is the data of custom fields sent to the backend when the form is submitted?

When the user submits a form with custom fields, the value of each custom field will be transmitted through its HTML element,nameproperty (i.e., the 'calling field' set in the backgrounditem.FieldName) as the key, send the user input or selected data to the background.The Anqi CMS backend's message management feature will automatically identify these fields and store the data for easy viewing and management.

Related articles

How to use the article's `flag` attribute (such as 'Top Story', 'Recommendation') to filter and highlight specific content?

In AnQi CMS, efficiently managing and highlighting specific content is the key to website operation success.When the content of the website becomes increasingly rich, how to ensure that important information can quickly attract the attention of visitors and guide them to browse the core content, which has become a problem that every operator needs to think deeply about.The Anqi CMS provides a powerful "recommendation attribute" (flag) feature that allows you to easily filter and highlight specific content, thereby enhancing user experience and website operation effects.### Understanding the `flag` attribute of Anqi CMS The `flag` attribute, as the name suggests

2025-11-08

How to obtain and display the contact information configured on the back-end, such as phone numbers, email addresses, WeChat QR codes, and other diversified information?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system that excels in helping enterprises display their brand image and provide convenient communication channels.For any website, having clear and easily accessible contact information is crucial for building trust with users and promoting cooperation.AnQi CMS deeply understands this, providing a set of intuitive and powerful functions that allow you to easily manage and display various contact information such as phone numbers, email addresses, WeChat QR codes, etc.### Backend Configuration: Central Management Center for Contact Information In AnQi CMS

2025-11-08

How to retrieve and display the global settings information of a website, such as the website name, Logo, and filing number in the template?

When building website templates in AnQiCMS, we often need to display some general information at the website level, such as the name of the website, Logo image, and legal filing number required by law.This information is usually managed in the background global settings to ensure consistency across the entire site and ease of maintenance.AnQiCMS has a powerful template tag system, especially the `system` tag, which makes it very intuitive and efficient to obtain and display these global settings in the template.### Understanding Global Settings and `system`

2025-11-08

On the article detail page, how to obtain and display a list of recommended articles related to the current article?

In website content operation, improving user stay time and reducing bounce rate is the goal pursued by every operator.After a user reads an article, if they can immediately see other content that interests them, it will undoubtedly greatly enhance their engagement, thereby improving the overall interactivity and user experience of the website.While AnQiCMS (AnQiCMS) is a powerful content management system, it provides us with convenient tools to implement intelligent recommendations for article detail pages without complex development work.The list of recommended articles not only provides users with a deeper reading experience, but also guides them to browse more website content

2025-11-08

How to format an article's publish timestamp into a user-friendly date and time format, such as '2023 January 01 14:30'?

User-friendly date and time display is crucial for website content.A clear and readable time format not only improves the visitor's reading experience, but also helps them quickly understand the publishing or updating timeliness of the content.In AnQiCMS, formatting the publication timestamp of articles into a format such as '2023-01-01 14:30' is both flexible and efficient.Understanding the use of timestamps in AnQiCMS In the content managed by AnQiCMS, whether it is articles, products, or other custom models

2025-11-08

How to display a dynamic banner image on the homepage of a website and implement click-through?

Displaying dynamic banner images with click-through functionality on the website homepage is a common need to enhance the visual appeal and user interaction of the site.By utilizing the powerful functions and flexible template mechanism of AnQiCMS, we can easily achieve this goal.The entire process is mainly divided into two parts: background configuration and front-end template invocation. ### First Step: Configure Banner Image and Link To display a dynamic rotating Banner on the website homepage, you first need to upload the image and set the link in the AnQiCMS backend. Typically

2025-11-08

How can AnQi CMS achieve automatic image compression and conversion to WebP format to optimize page loading performance?

Website images are important elements to enhance the attractiveness of content, but if not handled properly, they may also become the 'culprits' that slow down the website loading speed.For website operators, optimizing image loading performance can not only improve user experience but also have a positive impact on search engine rankings (SEO).AnQi CMS is an efficient content management system that fully considers this point, providing users with convenient and powerful image optimization features, especially automatic compression and WebP format conversion, allowing the website to maintain image quality while having faster loading speed.###

2025-11-08

How to determine if a variable (such as the article title) exists or is empty in a template and display a custom default value?

It is crucial to ensure the integrity and user experience of the content in website management.Sometimes, due to missing content or unassigned variables, blank areas or error messages may appear on web pages, which undoubtedly affects the professionalism of the website.The template engine of AnQiCMS provides a flexible and powerful mechanism to help users elegantly handle these situations, ensuring robust presentation of page content even if variables do not exist or are empty.In AnQiCMS template design, we often encounter situations where we need to display a variable (such as article title, description, image URL, etc.)

2025-11-08