How to integrate the comment submission form and handle user comments?

Calendar 78

In modern website operations, user comments are an important way to enhance the interaction and activity of content.AnQiCMS (AnQiCMS) provides a powerful and flexible comment feature that allows you to easily integrate a comment submission form on your website and effectively manage user comments.Next, we will explore how to achieve this goal in AnQiCMS.

I. Preparations and overview of the comment feature in the background

Before integrating the comment form, we first need to confirm that the comment feature is enabled in the background.AnQi CMS usually concentrates such interactive features in the 'Function Management' module.Here, you can find the 'Content Comments' option, to enable and disable the comment feature as a whole, as well as some basic comment rule settings.

In addition, to ensure the security of comment submission and avoid the泛滥 of spam comments, it is strongly recommended to enable captcha functionality.You can find and enable the captcha option under the "Content Settings" in the "Background Settings".After enabling, when integrating the comment form into the template, additional code related to the captcha needs to be added to coordinate with the backend settings.The "Content Comments" management page provided by the background, which is also the main battlefield for your future review, editing, and deletion of user comments.

II. Integrating the comment submission form in the template

Integrating the comment submission form into the detail page of the website (such as the article detail page or product detail page) is the first step for users to post comments.The Anqi CMS template system is very flexible, allowing you to build forms with simple tags and HTML structure.

usually, the comment form is placed in the detail page template file, for example{模型table}/detail.html. You need to build a standard HTML<form>element and specify its submission address as/comment/publish.

the core elements of the form include:

  • Hidden fieldarchive_id: This is the identifier associated with specific content in the comment. You can usearchiveDetailtags to dynamically retrieve the document ID of the current page, for example{% archiveDetail with name="Id" %}, and use it asarchive_idthe value.
  • Hidden fieldreturnThis field is used to specify the data format returned by the backend. It is usually set tohtmlorjson. If you want the page to refresh and display a success message, you can usehtml; Set to asynchronous processing through JavaScriptjson.
  • Usernameuser_name: This is a text input box for the user to fill in their nickname.
  • Comment contentcontentThis is a multi-line text field(textarea) for users to enter comments.
  • Parent comment IDparent_id(Optional)When a user replies to a specific comment, this field is needed to establish a parent-child relationship. It is usually also a hidden field, set dynamically through JavaScript.

Integration of captcha

If the backend has enabled captcha, then the form must contain fields related to captcha. This usually includes a hiddencaptcha_idfield, one for user input.captchaText box, as well as a display of the captcha image<img>Label. Additionally, a JavaScript code is required to request the captcha image and refresh the image when the user clicks it.

For example, you can add the following code to the form:

<form method="post" action="/comment/publish">
  <input type="hidden" name="return" value="html">
  <input type="hidden" name="archive_id" value="{% archiveDetail with name="Id" %}">
  <input type="hidden" name="parent_id" value="0" id="comment-parent-id"> {# 默认为0,回复时动态修改 #}

  <div>
    <label for="user_name">您的昵称</label>
    <input type="text" name="user_name" id="user_name" required placeholder="请填写您的昵称">
  </div>

  <div>
    <label for="content">评论内容</label>
    <textarea name="content" id="content" required rows="5" placeholder="请留下您的宝贵评论"></textarea>
  </div>

  {# 验证码区域,如果后台启用 #}
  <div class="captcha-area">
    <input type="hidden" name="captcha_id" id="captcha_id">
    <input type="text" name="captcha" required placeholder="请填写验证码">
    <img src="" id="get-captcha" alt="验证码" title="点击刷新验证码">
    <script>
      // 推荐使用原生JS或您网站已引入的jQuery
      document.getElementById('get-captcha').addEventListener("click", function () {
        fetch('/api/captcha')
          .then(response => response.json())
          .then(res => {
            document.getElementById('captcha_id').value = res.data.captcha_id;
            document.getElementById('get-captcha').src = res.data.captcha;
          })
          .catch(err => console.error('获取验证码失败:', err));
      });
      document.getElementById('get-captcha').click(); // 页面加载时自动获取一次验证码
    </script>
  </div>

  <button type="submit">提交评论</button>
  <button type="reset">重置</button>
</form>

3. Display the user comment list

After submitting the form, users naturally want to see the comments that have been published. In the detail page template, use the CMS provided by Anqi.commentListTags, it can be very convenient to get and display the comment list.

Use{% commentList comments with archiveId=archive.Id type="page" limit="10" %}Such tags, you can:

  • ByarchiveIdThe parameter specifies which document's comments to retrieve. This is also used here.archive.IdTo dynamically obtain the current document ID.
  • type="page"Indicates that you want the comment list to support pagination, so that it can be配合paginationTag display pagination navigation.
  • limit="10"It controls the number of comments displayed per page.

ByforLoop throughcommentsVariable, you can display the detailed information of each comment, such asitem.UserName(Commenter nickname),item.Content(Comment content) anditem.CreatedTime(Comment time).stampToDateThe filter can format timestamps into a readable date and time format.

AnQi CMS provides for the reply feature,item.ParentField, if the comment is a reply to another comment, this field will contain information about the replied comment, making it easier for you to build nested comment display styles. At the same time,item.StatusThe field can be used to determine whether a comment has passed review, you can decide whether to display or prompt the review status according to its value (Status = 1Indicates that it has been reviewed)})

Example of comment list and pagination:

`html

User Comments ({{archive.CommentCount}})

{# Show comment count #} {% commentList comments with archiveId=archive.Id type=“page” limit=“10” %}

{% for item in comments %}
  {% if item.Status == 1 %} {# 只显示已审核的评论 #}
    <div class="comment-item">
      <div class="comment-meta">
        <span>{{ item.UserName }}</span>
        <span>- {{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</span>
      </div>
      {% if item.Parent %}
        <blockquote class="reply-to">
          回复 {{ item.Parent.UserName }}: {{ item.Parent.Content|truncatechars:50 }}
        </blockquote>
      {% endif %}
      <p>{{ item.Content }}</p>
      <div class="comment-actions">
        <a href="javascript:;" class="reply-btn" data-comment-id="{{item.Id}}" data-user-name="{{item.UserName}}">回复</a>
        <a href="javascript:;" class="like-btn" data-comment-id="{{item.Id}}">赞 (<span>{{item.VoteCount}}</span>)</a>
      </div>
    </div>
  {% else %}
    <div class="

Related articles

How to display the user comment list on the article detail page?

User comments are an indispensable part of the website content ecosystem. They not only effectively enhance content interactivity and user engagement, but also bring a richer UGC (User Generated Content) to the website, helping with SEO optimization and fostering a community atmosphere.AnQiCMS as a powerful content management system has fully considered this need, providing you with a flexible and efficient way to integrate the user comment list into your article detail page.

2025-11-09

How to display the WeChat QR code of the website or customize social media links?

In today's digital marketing era, the close integration of websites and social media is crucial for enhancing user interaction and brand influence.AnQiCMS as an efficient content management system fully considers the user's needs and provides a flexible and convenient way to display the WeChat QR code of the website and various custom social media links.In order to make it convenient for users to add WeChat consultation or guide them to visit your Facebook, Twitter, and other platforms, AnQiCMS can help you easily achieve this, making your website a bridge connecting users.### Step 1

2025-11-09

How to display the contact information, phone number, address, and other contact methods set on the website?

In modern website operation, clearly displaying the company's contact information is a key link in building trust and facilitating communication with users.AnQiCMS (AnQiCMS) knows this well, providing you with an intuitive and flexible backend settings, allowing you to easily manage and display the contact information, phone number, address and other information of the website.This article will introduce in detail how to set and display these important contact information in Anqi CMS.### One, configure your contact information in the Anqi CMS backend All contact information displayed on the website front end first needs to be unified managed in the Anqi CMS backend

2025-11-09

How to display breadcrumb navigation on a page to improve user experience?

In modern web design, breadcrumb navigation has become a standard element to enhance user experience and website usability.It acts as a guide for users to advance, clearly showing the user's current position in the website's hierarchy structure and providing a convenient way to return to the parent page.For websites built using AnQiCMS, integrating breadcrumb navigation can greatly facilitate visitors and also play a positive role in search engine optimization (SEO).Why is breadcrumbs navigation so important? Imagine

2025-11-09

How to implement the like function for comments?

User interaction is an important manifestation of website vitality, which can significantly enhance the attractiveness and user stickiness of content.AnQi CMS is a powerful and highly customizable content management system that provides a rich set of features to enhance user engagement.Among them, the comment function is the core battlefield of user communication, and adding a like function to the comments can undoubtedly further stimulate community vitality and allow users to give more direct and positive feedback to high-quality comments.### Overview of AnQi CMS Comment Function AnQi CMS is built-in with a comprehensive comment management system, allowing website visitors to easily comment on articles

2025-11-09

How to build a custom website contact form?

The website feedback form is an important bridge for interaction between the website and visitors. Whether it is to collect user feedback, customer inquiries, or other interactive needs, a well-designed and functional feedback form is crucial.In Anqi CMS, building a custom message form is much simpler and flexible than you imagine.The built-in 'website message' function provides a powerful basic framework, allowing us to customize deeply according to actual needs. Next, we will discuss in detail how to build a custom message form in Anqi CMS that meets your specific needs.

2025-11-09

How to add captcha functionality to a message or comment form?

During the operation of the website, the message and comment function is an important channel for enhancing user interaction and collecting feedback.However, the spam comments and malicious submissions that follow often bother website administrators.To effectively prevent these unexpected guests, adding a captcha function to the comment or message form is particularly important.AnQi CMS as a comprehensive content management system provides a convenient way to integrate this security mechanism, helping you maintain a clean, efficient interactive platform.### Preparation Before adding a captcha to your comment or message form

2025-11-09

How to display the friend link list of the website?

During the operation of a website, friendship links are a common way for websites to recommend each other and share traffic. They not only help to increase the number of external links on the website, but also have a positive effect on SEO optimization, and can provide visitors with more valuable resources.AnQiCMS (AnQiCMS) provides a very convenient function for us to manage and display friend links. ### Managing Friend Links: Getting Started with Backend Operations Managing friend links in AnQi CMS is very intuitive.First, we need to log in to the website backend, find the "Function Management" item in the left function menu.

2025-11-09