How to dynamically generate and display the user's guestbook form with the label `guestbook`?

Calendar 👁️ 70

In website operation, visitor comments and interaction are important for building trust, obtaining feedback, and promoting business development.AnQiCMS (AnQiCMS) is well aware of this need, and therefore provides a flexible and powerful form message function that allows you to easily generate and display user message forms on your website.This is mainly through its built-inguestbookTo implement template tags, it not only allows you to customize form fields but also ensures the security and smooth submission of forms.

UnderstandingguestbookThe core role of tags

When we need to add a comment form on the website,guestbookThe tag does not directly generate a complete HTML form, but provides an array containing all the field *definitions*. This means that you can flexibly build a comment form that conforms to the design style of your website, rather than being restricted by fixed styles.This design concept allows for highly customizable form presentation, fully reflecting the advantages of AnQiCMS in content model and modular design.

The definition of these form fields comes from the 'Function Management' of the AnQiCMS background under the 'Website Message' settings.Here, you can add or edit the various fields required for the message form, such as visitor name, contact information, message content, and even customize other fields according to business needs, such as 'intended product', 'service type', etc.Each custom field will have its corresponding display name, field type (such as text, number, multiline text, radio, checkbox, or dropdown selection) and whether it is required or not properties.

Front-end application: Dynamically build a message form

To convert these backend-defined fields into actual usable forms in your template, we will do so in the corresponding template file (for example inguestbook/index.htmlOr other pages that need to display the comment form are usedguestbookTags:

{% guestbook fields %}
    <form method="post" action="/guestbook.html">
        {# 这里将根据fields数组动态生成表单字段 #}
        {% for item in fields %}
            <div>
                <label for="{{ item.FieldName }}">{{ item.Name }}</label>
                <div>
                    {% if item.Type == "text" or item.Type == "number" %}
                    <input type="{{ item.Type }}" name="{{ item.FieldName }}" id="{{ item.FieldName }}"
                           {% if item.Required %}required{% endif %}
                           placeholder="{{ item.Content }}" autocomplete="off">
                    {% elif item.Type == "textarea" %}
                    <textarea name="{{ item.FieldName }}" id="{{ 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 }}" id="{{ item.FieldName }}_{{ forloop.Counter }}">
                        <label for="{{ item.FieldName }}_{{ forloop.Counter }}">{{ val }}</label>
                        {% endfor %}
                    {% elif item.Type == "checkbox" %}
                        {% for val in item.Items %}
                        <input type="{{ item.Type }}" name="{{ item.FieldName }}[]" value="{{ val }}" id="{{ item.FieldName }}_{{ forloop.Counter }}">
                        <label for="{{ item.FieldName }}_{{ forloop.Counter }}">{{ val }}</label>
                        {% endfor %}
                    {% elif item.Type == "select" %}
                    <select name="{{ item.FieldName }}" id="{{ item.FieldName }}">
                        {% for val in item.Items %}
                        <option value="{{ val }}">{{ val }}</option>
                        {% endfor %}
                    </select>
                    {% endif %}
                </div>
            </div>
        {% endfor %}

        {# 验证码区域,如果开启了验证码功能 #}
        <div style="display: flex; align-items: center; margin-bottom: 15px;">
            <input type="hidden" name="captcha_id" id="captcha_id">
            <input type="text" name="captcha" required placeholder="请填写验证码" style="flex: 1; padding: 10px; border: 1px solid #ccc; border-radius: 4px;">
            <img src="" id="get-captcha" style="width: 120px; height: 40px; cursor: pointer; margin-left: 10px; border-radius: 4px; border: 1px solid #eee;" alt="验证码" />
            <script>
                document.getElementById('get-captcha').addEventListener("click", function () {
                    fetch('/api/captcha')
                            .then(response => response.json())
                            .then(res => {
                                document.getElementById('captcha_id').setAttribute("value", res.data.captcha_id);
                                document.getElementById('get-captcha').setAttribute("src", res.data.captcha);
                            })
                            .catch(err => console.error('获取验证码失败:', err));
                });
                // 页面加载时自动获取一次验证码
                document.getElementById('get-captcha').click();
            </script>
        </div>

        <div>
            <button type="submit" style="padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">提交留言</button>
            <button type="reset" style="padding: 10px 20px; background-color: #6c757d; color: white; border: none; border-radius: 4px; cursor: pointer; margin-left: 10px;">重置</button>
        </div>
    </form>
{% endguestbook %}

In this code block:

  1. {% guestbook fields %}The tag will load the form fields defined in the backgroundfieldsthe variable.
  2. fieldsA variable is an array that contains the detailed information of each input item defined for the comment form in the background.
  3. We pass{% for item in fields %}looping through this array. In each loop,itemA variable represents the definition of a form field.
  4. Based onitem.Type(Form type), we use conditional judgment{% if item.Type == "text" %}to render different HTML form elements such as<input type="text">/<textarea>/<input type="radio">/<select>etc.
  5. item.Nameused to display the label text of the form itemitem.FieldNamethen as the HTML element'snameandidProperty ensures that form data can be submitted correctly and interact with the front-end.
  6. item.RequiredProperty determines whether to add HTML's.requiredProperty implements front-end required validation.
  7. For single-choice, multiple-choice, and dropdown fields,item.Itemsan option list is provided, and we loop through again{% for val in item.Items %}to generate these options.

Submit message: Make sure the data is sent

Related articles

How to get and display the comments made by users on the content for the comment list label `commentList`?

In today's digital world, websites are not just platforms for displaying information, but also spaces for user interaction and communication.When users can express their opinions and ask questions about the content, the vitality of the website will be greatly enhanced.Anqi CMS knows this and therefore provides a powerful and flexible comment management feature, where the 'comment list tag `commentList`' is the key bridge connecting content and user feedback. Imagine that you have published an engaging article or an innovative product, and users can't wait to share their thoughts after reading it.As a website operator

2025-11-07

How to implement the labeling display of document Tag list label `tagList`, detail label `tagDetail`, and document list label `tagDataList`?

In website operation, efficient content management and flexible content display are the key to improving user experience and search engine performance.AnQiCMS (AnQiCMS) is well-versed in this, providing powerful tag features to help us easily implement the labeling and display management of content.By skillfully utilizing `tagList`, `tagDetail`, and `tagDataList` as these core tags, we can give wings to the website content, making information organization more scientific, user search more convenient, and also lay a solid foundation for SEO optimization.Core

2025-11-07

How to implement custom attribute display and filtering for document parameter tag `archiveParams` and filter tag `archiveFilters`?

In AnQi CMS, displaying and filtering custom attributes of content is the core of implementing flexible and personalized website functions.By using the `archiveParams` and `archiveFilters` template tags, we can easily display custom content fields on the frontend and provide users with powerful functionality for filtering content based on these fields, thus greatly enhancing the website's user experience and content discoverability.### `archiveParams` Tag: Flexible Display of Custom Properties In AnQi CMS

2025-11-07

How to implement navigation display between content for previous/next document tags `prevArchive`/`nextArchive`?

In website operation, providing readers with a smooth reading experience is the key to enhancing user satisfaction and website value.After a user finishes reading an exciting article or product introduction, if they can conveniently see related content such as 'Previous' or 'Next', it will undoubtedly greatly increase their time spent on the website, forming a more natural browsing path, which is very beneficial for SEO optimization and content dissemination.AnQi CMS, as a powerful content management system, fully understands this need

2025-11-07

How to display the link list of cooperative website links with the tag `linkList`?

In website operation, friendship links (also known as cooperative website links) play an important role.They not only bring additional traffic to the website, but also help to improve search engine optimization (SEO) performance, enhance the authority and user trust of the website.In Anqi CMS, managing and displaying these links becomes very convenient, mainly due to its powerful template tag system, especially the `linkList` tag.### Flexibly display cooperative websites: `linkList` tag usage and basic principles `linkList`

2025-11-07

How does the pagination tag `pagination` provide navigation and display control for long content lists?

In website operation, when our content list (whether it is articles, products, or other information) accumulates to a certain amount, displaying all content on a single page not only significantly affects the page loading speed but may also make visitors feel overloaded with information, making it difficult for them to find the content they are really interested in.At this point, a reasonable pagination mechanism is particularly important. AnQiCMS provides a powerful and flexible `pagination` tag, which helps us provide clear navigation and display control for long content lists, thereby greatly optimizing user experience and website performance.Core Function

2025-11-07

How `if` logical judgment tags and `for` loop iteration tags control the conditional display and repeated display of content?

In the daily operation of Anqi CMS, we often need to decide which content should be displayed according to different business logic, as well as how to efficiently repeat the display of a large amount of similar content.At this time, the `if` logical judgment tag and the `for` loop traversal tag have become indispensable tools in template design.They allow us to flexibly control the conditional display and repetition of content, greatly enhancing the dynamicity and maintainability of the website.### The conditional display tool - `if` logical judgment tag The `if` tag is the basis for conditional content display in Anqi CMS templates

2025-11-07

How to format the timestamp label `stampToDate` to display time data in a readable format?

In a content management system, time data often exists in the form of a series of numbers, which we call a Unix timestamp.This form is very efficient for machine processing, but for us users, a string like `1678886400` is obviously not intuitive to understand the date and time it represents.AnQi CMS knows this point well, therefore it provides a very practical template tag `stampToDate`, which helps us convert these cryptic timestamps into the date and time format we are accustomed to reading.###

2025-11-07