How to use Ajax to asynchronously submit AnQiCMS message form data on the front end?

Calendar 👁️ 58

As a senior website operations expert, I know the importance of user experience in the success of a website.In today's fast-paced online environment, users have increasingly high expectations for page loading speed and smooth interaction.The traditional way of submitting a message form often leads to page refresh, which not only interrupts the user's operation process, but may also cause unnecessary waiting due to network latency, thereby affecting user satisfaction.

Fortunately, with the continuous development of front-end technology, we can use Ajax (Asynchronous JavaScript and XML) to submit data asynchronously, complete form submission without refreshing the page, and significantly improve the user experience.Today, let's delve into how to elegantly implement the Ajax asynchronous submission of the comment form on the front end of AnQiCMS (AnQiCMS).


Say goodbye to waiting: Implement the Ajax asynchronous submission of the留言表单 in AnQiCMS frontend elegantly

AnQiCMS is the preferred choice for many enterprise content management systems with its high efficiency, customizability, and SEO-friendliness.Its flexible template engine and rich tag features provide great convenience for front-end developers.The comment form is an important bridge for website interaction with users. Using Ajax asynchronous submission can not only optimize the user experience but also reduce server pressure and improve data processing efficiency.

Preparation: Understand the operation mechanism of the AnQiCMS留言表单

Before implementing Ajax submission on the front end, we need to understand how the AnQiCMS message form works in the background. According to the AnQiCMS documentation, the message form data is submitted by default to/guestbook.htmlThis URL. It is noteworthy that AnQi CMS supports onereturnParameters control the data format of the server's response. For Ajax submissions, we usually want the server to return JSON-formatted data so that front-end JavaScript can easily parse and process.

AnQi CMS template tags{% guestbook fields %}Can dynamically generate the background configuration of the message form fields. These fields include but are not limited to:

  • user_name: The name of the message sender
  • contactContact Information
  • contentMessage Content
  • And various fields customized in the background.

In addition, if the background has enabled the message verification code function, the form must also includecaptcha_idandcaptchaTwo fields, as well as an image element used to refresh the captcha.

Build the HTML structure of the front-end message form

First, in your security CMS template file (for exampleguestbook/index.htmlOr you can create a custom feedback page template) and add a basic HTML form. It is recommended to add a form element for easy JavaScript access.id. It is crucial to ensure that all input fields have the correctnameproperties, thesenameproperties need to match the field names expected by the AnQiCMS backend.

This is an example of an HTML form structure that includes dynamic fields and captcha:

<form id="guestbookForm" method="post" action="/guestbook.html">
    <!-- 动态生成的留言字段 -->
    {% guestbook fields %}
        {% for item in fields %}
        <div class="form-group">
            <label for="{{ item.FieldName }}">{{ item.Name }}{% if item.Required %}<span class="text-danger">*</span>{% endif %}</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"
                       class="form-control">
                {% elif item.Type == "textarea" %}
                <textarea name="{{ item.FieldName }}" id="{{ item.FieldName }}"
                          {% if item.Required %}required{% endif %} placeholder="{{ item.Content }}" rows="5"
                          class="form-control"></textarea>
                {% elif item.Type == "radio" %}
                    {% for val in item.Items %}
                    <input type="{{ item.Type }}" name="{{ item.FieldName }}" value="{{ val }}" id="{{ item.FieldName }}-{{ loop.index }}">
                    <label for="{{ item.FieldName }}-{{ loop.index }}">{{ val }}</label>
                    {% endfor %}
                {% elif item.Type == "checkbox" %}
                    {% for val in item.Items %}
                    <input type="{{ item.Type }}" name="{{ item.FieldName }}[]" value="{{ val }}" id="{{ item.FieldName }}-{{ loop.index }}">
                    <label for="{{ item.FieldName }}-{{ loop.index }}">{{ val }}</label>
                    {% endfor %}
                {% elif item.Type == "select" %}
                <select name="{{ item.FieldName }}" id="{{ item.FieldName }}" class="form-control">
                    {% for val in item.Items %}
                    <option value="{{ val }}">{{ val }}</option>
                    {% endfor %}
                </select>
                {% endif %}
            </div>
        </div>
        {% endfor %}
    {% endguestbook %}

    <!-- 验证码部分 (如果后台开启) -->
    <div class="form-group captcha-section">
        <label>验证码<span class="text-danger">*</span></label>
        <div class="input-group">
            <input type="hidden" name="captcha_id" id="captchaId">
            <input type="text" name="captcha" id="captchaInput" required placeholder="请输入验证码" class="form-control captcha-input">
            <img src="" id="captchaImage" alt="验证码" class="captcha-img">
        </div>
    </div>

    <!-- 重要的隐藏字段:告诉AnQiCMS我们想要JSON响应 -->
    <input type="hidden" name="return" value="json">

    <div class="form-group">
        <button type="submit" id="submitBtn" class="btn btn-primary">提交留言</button>
        <button type="reset" class="btn btn-secondary">重置</button>
    </div>
</form>

<div id="responseMessage" class="mt-3"></div>

<script>
    // 验证码刷新逻辑 (来自tag-/anqiapi-other/167.html文档)
    function refreshCaptcha() {
        fetch('/api/captcha')
            .then(response => response.json())
            .then(res => {
                if (res.code === 0) {
                    document.getElementById('captchaId').value = res.data.captcha_id;
                    document.getElementById('captchaImage').src = res.data.captcha;
                } else {
                    console.error('Failed to get captcha:', res.msg);
                }
            })
            .catch(err => console.error('Error fetching captcha:', err));
    }

    // 页面加载时初始化验证码
    document.addEventListener('DOMContentLoaded', refreshCaptcha);
    // 点击验证码图片时刷新
    document.getElementById('captchaImage').addEventListener('click', refreshCaptcha);
</script>

Implement JavaScript logic for Ajax asynchronous submission

Next, we will use JavaScript (here using the nativefetchFor example, it is more modern and does not require libraries) to handle form submission events. The core idea is to prevent the table

Related articles

Does the AnQiCMS message form provide an API interface for external systems to submit data?

As an experienced website operations expert, I am well aware of the importance of data interconnection in modern website operations.Many AnQi CMS users, especially those who want to import or link external system data to the CMS, may be concerned about a specific issue: Does the AnQi CMS message form provide an API interface for external systems to submit data?After an in-depth study of the AnQiCMS features, combined with the provided documentation information, I will explain this issue to you in detail.

2025-11-06

How can historical message data be batch deleted or archived in AnQiCMS?

## Mastering AnQi CMS: Practical Guide to Efficiently Manage Historical Comments, Bulk Delete and ArchiveFor any website dedicated to providing a high-quality online experience, it is crucial to manage these historical data properly.On the one hand, timely cleaning up redundant or outdated comments can effectively improve website performance and optimize user experience;On the other hand, a reasonable archiving strategy can also ensure that important information is preserved and meet data compliance requirements.AnQi CMS as an efficient

2025-11-06

The custom field of the comment form, such as the 'City' option, can it be dynamically read from the database?

As an experienced website operation expert, I know the importance of a flexible and efficient content management system in the increasingly complex network environment.AnQiCMS (AnQiCMS) boasts its high-performance architecture based on the Go language and rich features, demonstrating unique advantages in the content management field.Today, let's delve into a common issue in website operations: custom fields in comment forms, such as the 'city' option we often use to collect user information, can it be dynamically read from the database?

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

Can AnQiCMS automatically identify and fill in user login information (such as username, email)?

## Deep Analysis of Automatic Identification and Filling of Login User Information in Anqi CMS Comment Form As an experienced website operations expert, I fully understand the core position of user experience in content management.When visitors come to our website, whether it is for consultation, feedback, or interaction, the comment form is an important bridge.Many operators have pondered such a question: **Can the AnQiCMS feedback form automatically identify and fill in user login information, such as their username and email?

2025-11-06

How to use the `Required` field in AnQiCMS template to generate the HTML5 `required` attribute?

As an experienced website operations expert, I know that building an efficient and user-friendly website not only lies in its rich content, but also in its excellent user experience.In a multitude of content management systems, AnQiCMS stands out with its powerful custom content model and flexible template engine, especially in handling user interactions, such as form submissions, where it can easily meet fine-grained requirements.

2025-11-06

How to manage and set the `placeholder` text of the AnQiCMS message form through the backend?

In the many details of website operation, the design and management of the message form often reflect the professionalism and user-friendliness of the website.A well-designed feedback form that can effectively collect information and enhance user experience.Among them, the `placeholder` text may seem trivial, but it plays an indispensable role in guiding user input and enhancing the clarity of forms.Today, let's delve deeply into how AnQiCMS (AnQiCMS) achieves unified configuration and optimization of the `placeholder` text for the comment form through its powerful backend management system.

2025-11-06

How to add a checkbox for privacy policy or terms of service in the comment form?

## Enhancing Website Compliance: How to Add a Privacy Policy Checkbox Gracefully to Anqi CMS Contact Form In the digital age, user privacy protection has become a cornerstone that cannot be ignored in website operations.Whether it is to deal with the EU's GDPR or similar regulations in other regions, clearly informing users of the data processing methods and obtaining their consent is an important step in building trust and avoiding risks.

2025-11-06