How to display the message form submitted by users on the website?

Calendar 👁️ 59

AnQi CMS provides an efficient and flexible mechanism to easily display and manage user message forms on your website.Whether it is to collect user feedback, consult, or simple interactive messages, Anqi CMS can quickly help you achieve this function and customize it according to business needs.

Learn about the Anqi CMS message function

In the Anqi CMS backend management interface, you can find the 'Website Messages' option under the 'Function Management' menu.This is the core area where you manage all comment-related settings, including viewing submitted comments, customizing form fields, and configuring the behavior after comment submission.

The AnQi CMS message form feature has a major highlight in supporting custom fields.This means you can add various types of form elements as needed, such as text boxes, number inputs, multi-line text, radio buttons, checkboxes, and dropdown selections.These custom fields are dynamically recognized and rendered in the front-end template, greatly enhancing the flexibility and applicability of the form.

In the website front-end template, the comment form is usually placed on a special comment page, such as the default one.guestbook/index.htmlorguestbook.htmlIf you are developing a custom template, you can in the file./templateCreate or edit these files in the template folder under the directory.

Display the message form in the template

To display the message form on the website page, we need to rely on the Anqi CMS provided.guestbookLabel. This label will prepare all the message field information you configure in the background and store them in a namefields.

The following is an example of code to dynamically generate a message form in the template

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

The core logic of this code is to use{% for item in fields %}Loop through each field defined in the background comments. Inside the loop, based onitem.Typethe value (for exampletext/textarea/radioDynamic rendering of the corresponding HTML form elements. Each form element'snameattribute is set toitem.FieldName. This is an important basis for the backend to recognize and process the data.item.RequiredThe attribute is used to determine whether the field is required and to add the correspondingrequiredProperty.

In addition to the dynamically generated custom fields, the message form also needs several basic fields for users to submit information:

  • user_name: User's Name
  • contact: User's Contact Information (such as phone, email, etc.)
  • content: Specific Content of the Message

These basic fields are not directly reflected in the dynamically generated code above. Typically, you can configure them in the background message form settings. If you want these fields to be presented in a customized way, you can manually do so inguestbookAdd these outside the label loopinputortextareaelements, making sure theirnameproperties meet backend requirements (for examplename="user_name")

For example, you can manually add basic username and contact information fields like this:

<form method="post" action="/guestbook.html">
  <input type="hidden" name="return" value="html"> {# 可选:指定后端返回html格式 #}
  <div>
    <label>您的姓名</label>
    <div>
      <input type="text" name="user_name" required placeholder="请填写您的姓名" autocomplete="off">
    </div>
  </div>
  <div>
    <label>您的联系方式</label>
    <div>
      <input type="text" name="contact" required placeholder="请填写您的手机号或邮箱" autocomplete="off">
    </div>
  </div>
  <div>
    <label>留言内容</label>
    <div>
      <textarea name="content" required placeholder="请输入您的留言" rows="5"></textarea>
    </div>
  </div>
  {# 动态生成的自定义字段,如同上述代码片段 #}
  {% guestbook fields %}
      {# ... 动态字段渲染逻辑 ... #}
  {% endguestbook %}
  <div>
      <button type="submit">提交留言</button>
      <button type="reset">重置</button>
  </div>
</form>

The submission and verification of the message form

The message form'sactionattribute should point to/guestbook.html, and usePOSTMethod submission. After the user fills in and submits the form, Anqi CMS will automatically receive and process these data.

To enhance the security of the website and prevent spam submissions, it is strongly recommended to enable the CAPTCHA function for the message form.

Add CAPTCHA (CAPTCHA)

  1. Enable CAPTCHA in the backgroundFirst, you need to find and enable the comment verification code feature in the 'Background Settings' -> 'Content Settings' of the Anqi CMS background.

  2. Template integrated verification codeIn your comment form HTML, find the appropriate location (such as above the submit button) and insert the following code snippet.This code is responsible for loading and displaying the captcha image, and handling the refresh logic of the captcha.

    <div style="display: flex; clear: both">
      <input type="hidden" name="captcha_id" id="captcha_id">
      <input type="text" name="captcha" required placeholder="请填写验证码" class="layui-input" style="flex: 1">
      <img src="" id="get-captcha" style="width: 150px;height: 56px;cursor: pointer;" />
      <script>
        // 普通 JavaScript 版本
        document.getElementById('get-captcha').addEventListener("click", function (e) {
          fetch('/api/captcha')
                  .then(response => {
                    return 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.log(err)})
        });
        document.getElementById('get-captcha').click(); // 页面加载时自动获取一次验证码
      </script>
      {# 如果您的网站使用 jQuery,可以使用以下代码替换上面的 script 标签内容 #}
      {#
      <script>
        // jQuery 版本
        $('#get-captcha').on("click", function (e) {
          $.get('/api/captcha', function(res) {
            $('#captcha_id').attr("value", res.data.captcha_id)
            $('#get-captcha').attr("src", res.data.captcha)
          }, 'json')
        })
        $('#get-captcha').click();
      </script>
      #}
    </div>
    

After integrating this code, your comment form will require users to enter a captcha and verify it upon submission, thereby effectively reducing interference from spam.

Summary

By following these steps, you have successfully displayed a fully functional and customizable user message form on the Anqi CMS website.The flexible content model of Anqi CMS and the powerful template tags make this process intuitive and efficient.Take advantage of the convenient management features of the backend, you can easily collect and respond to valuable feedback and suggestions from users, thereby better interacting with website visitors.


Frequently Asked Questions (FAQ)

Q1: I want to customize the message form fields and display them on the front end, how should I operate?A1: You can in the an

Related articles

How to implement the pagination display function of the article comment list?

In website operation, effectively managing and displaying user comments is crucial for enhancing user interaction and content vitality.AnQiCMS (AnQiCMS) is a powerful content management system that provides flexible template tags, allowing us to easily implement pagination display of article comment lists.This article will introduce in detail how to implement this feature through template tags in Anqi CMS, thus providing users with a better browsing experience.Understanding the core of comments and pagination In the Anqi CMS template system, implementing pagination display of article comment lists

2025-11-08

How to use Tag tags to associate and display related content on the front end?

In Anqi CMS, content organization and association is the key to enhancing website value, optimizing user experience, and improving search engine performance.In addition to the traditional classification structure, we also have a very powerful and flexible tool - Tag tag.It is like a 'living index' of content, which helps us to connect scattered content in multiple dimensions, making it easier for users to find the information they are interested in.Today, let's talk about how to make full use of Tag tags on the front end to associate and display our wonderful content.### Tag label: Flexible content association bridge Different from a strict classification system

2025-11-08

How to display the content of custom fields (such as author, source) on the article detail page?

In website operation, the article detail page is not only a place to display the core content, but also a key to convey additional information, enhance professionalism, and improve user experience.In addition to the title and body, we often need to display some custom fields on the article detail page, such as author, source, publisher, product model, and so on.This information not only enriches the content of the article, but also helps to enhance the authority and credibility of the website, even having a positive impact on SEO optimization and user decision-making.AnQiCMS (AnQiCMS) is designed with a flexible content model, making the display of these custom fields very intuitive and powerful

2025-11-08

How to get and display a list of recommended articles related to the current article?

In website operation, when we have worked hard to create an article, we naturally hope that it can be seen by more readers and guide them to explore more exciting content.On the bottom or sidebar of the article detail page, cleverly display recommended content related to the current article, which can not only significantly improve the depth and dwell time of users' reading, but also bring many benefits to search engine optimization (SEO).AnQiCMS (AnQiCMS) fully understands this need and provides extremely convenient and powerful tags to help us easily achieve this goal.### Cleverly Utilize `archiveList`

2025-11-08

How to call and display the friendship link set in the background on the front end?

AnQiCMS (AnQiCMS) provides a set of intuitive and powerful content management features, among which the management of 'friend links' is an indispensable part of website operation. It not only promotes the SEO performance of the website but also provides users with convenient navigation.This article will give a detailed introduction on how to call and elegantly display the friendship links you set in the background. ### Friend link backend configuration Managing friend links in Anqi CMS is very simple.You just need to log in to the back-end, find the "Function Management" module in the left menu bar, click to enter "Friend Links". Here

2025-11-08

How to implement pagination navigation display for content list in AnQi CMS?

When using AnQiCMS to build a website, it is particularly important to have reasonable pagination navigation when displaying a large amount of content, such as article lists, product lists, etc.It not only enhances user experience, making it easier for visitors to browse and find content, but also helps search engines better crawl the website.AnQi CMS provides a very intuitive and powerful pagination tag, making it easy to display content list pagination.### Step 1: Prepare the content list data To implement pagination of the content list, you first need to use `archiveList`

2025-11-08

How to embed mathematical formulas and flowcharts in a page and ensure correct rendering display?

In website operation, we often need to display some professional content, such as mathematical formulas involving scientific calculations, or complex flowcharts for business process descriptions.The traditional way of displaying this content is often inefficient, difficult to maintain, and also does not provide a good reading experience.AnQiCMS combines its powerful content management capabilities and support for Markdown to provide us with an elegant solution.By simply introducing some external libraries and enabling the Markdown editor, we can display the professional content correctly on the website page

2025-11-08

How to safely output HTML tags in website content to avoid escaping?

In website content management, especially when content includes rich formats or user input, how to safely output HTML tags is a crucial issue.AnQiCMS (AnQiCMS) fully considered this from the beginning, its template engine defaults to escaping output variables to effectively prevent cross-site scripting attacks (XSS) and other security risks.### Understanding the Default Behavior of Template Engines The Anqi CMS template engine follows the syntax of mainstream frameworks like Django, one of its core concepts being 'Security First'

2025-11-08