How to build a custom website contact form?

Calendar 👁️ 70

The website comment 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 comment 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 for in-depth customization according to actual needs.

Next, we will discuss in detail how to build a customized feedback form that meets your specific needs in Anqi CMS.

1. Configure the feedback form fields in the background.

To start building the message form, we first need to enter the Anqi CMS admin interface.Find and click on the left menu bar under "Function Management", then select "Website Message Management".This is the center of all comment form related settings.

1. Basic fields:Generally, the system provides some basic fields, such as visitor name (user_name)、Contact Information(contact) and message content (contentThese are enough to meet the basic needs of most websites' comments, and they are default existing; you do not need to create them extra.

2. Custom field:However, if your business has more unique visitor information collection needs, the strength of Anqi CMS lies in its flexible support for custom fields. You can add more fields according to actual circumstances, such as:

  • Text type (text): Used to collect brief text information, such as company name, position, etc.
  • Number type (number): Used to collect pure numeric information, such as age, budget amount, etc.
  • Multiline text (textarea): Used to collect detailed descriptive content, such as project requirements, suggestions, etc.
  • Single choice (radio)Provide preset options, visitors can only select one, for example "Gender", "Product type".
  • Multiple choice (checkbox)Provide preset options for visitors to select multiple items, such as 'services of interest' and 'product features.'
  • Dropdown selection (select)Provide a dropdown menu for visitors to select preset options.

When adding custom fields, you can set the field name (for backend and template display), the corresponding field variable name (for data storage and template calls), and specify whether the field is required.For radio, checkbox, and dropdown fields, you also need to set their default values, that is, the various options provided to the visitor, each on a separate line.

These settings are completed, and the data structure of the form has been set up in the background. The configuration in the background is intuitive and easy to operate, and it only takes a few steps to complete the addition, deletion, modification, and query of fields.

Create and customize the message form template

After the field configuration on the backend is complete, we need to present these fields on the website frontend to form a form that visitors can fill out.

1. Template file location:According to the template conventions of Anqi CMS, the template file for the comment form is usually located in your current template directory.guestbook/index.htmlIf the file does not exist, you can create it manually.

2. Core template tags:guestbook:Anqi CMS provides a specialguestbookTemplate tag, it can intelligently retrieve the message fields you define in the background. You can loop through the tag to return thefieldsvariable, dynamically generate form elements.

This is a basic template structure example of a message form:

<form method="post" action="/guestbook.html">
    {% guestbook fields %}
        {% for item in fields %}
        <div class="form-group">
            <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" 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 %}
                    <label>
                        <input type="{{ item.Type }}" 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="{{ item.Type }}" name="{{ item.FieldName }}[]" value="{{ val }}"
                               {% if item.Required %}required{% endif %}> {{ val }}
                    </label>
                    {% endfor %}
                {% elif item.Type == "select" %}
                <select name="{{ item.FieldName }}" id="{{ item.FieldName }}"
                        {% if item.Required %}required{% endif %} class="form-control">
                    {% for val in item.Items %}
                    <option value="{{ val }}">{{ val }}</option>
                    {% endfor %}
                </select>
                {% endif %}
            </div>
        </div>
        {% endfor %}

        <div class="form-group">
            <button type="submit" class="btn btn-primary">提交留言</button>
            <button type="reset" class="btn btn-secondary">重置</button>
        </div>
    {% endguestbook %}
</form>

In the code above:

  • <form method="post" action="/guestbook.html">Defines the form submission method and target address.
  • {% guestbook fields %}The label retrieves the form fields defined by the background and assigns them tofieldsVariable.
  • {% for item in fields %}Loop through each field.
  • {{ item.Name }}Display the Chinese name of the field.
  • {{ item.FieldName }}asinputortextareaofnameandidProperty value.
  • {% if item.Required %}required{% endif %}Automatically add HTML5 based on backend settings.requiredProperty.
  • placeholder="{{ item.Content }}"Use the default value set by the backend as the prompt text.
  • Based onitem.TypeDifferent, we have generated different HTML form elements such astext/number/textarea/radio/checkboxandselect. Especially for multiple choice, single choice and dropdown selection, we loop againitem.Itemsto generate each option.

By this means, any field added or modified on the backend will automatically update the frontend form, greatly improving maintenance efficiency.

III. Further Enhancement and Security Protection of the Form

A beautiful and functional comment form also needs to consider user experience and security.

1. Add captcha:In order to effectively prevent malicious submissions or spam, adding a captcha to the comment form is a very important measure.

  • Backend enabled: First, make sure that the captcha function is enabled in the "Function Management" -> "Website Message Management" of the Anqi CMS background.
  • Template integration:In yourguestbook/index.htmlIn the template, you need to add the captcha display area and the related JavaScript code.

This is an integrated captcha template code example (you can choose the native JS or jQuery version according to your front-end framework):

Native JavaScript version:

`twig

<input type="hidden" name="captcha_id" id="captcha_id">
<input type="text" name="captcha" required placeholder="请填写验证码" class="form-control" style="flex: 1; margin-right: 10px;">
<img src="" id="get-captcha" style="width: 150px; height: 40px; cursor: pointer; border-radius: 4px; border: 1px solid #ddd;" alt="验证码">
<script>
    document.getElementById('get-captcha').addEventListener("click", function () {
        fetch('/api/captcha')
            .then(response => response.json())
            .

Related articles

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 integrate the comment submission form and handle user comments?

In modern website operations, user comments are an important way to enhance the interactivity and activity of content.AnQiCMS (AnQiCMS) offers a powerful and flexible comment feature, allowing 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 together.

2025-11-09

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 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

How to implement pagination in the content list?

When managing website content, especially when the amount of content increases, a straightforward and efficient pagination feature becomes particularly important.It not only allows visitors to find the information they need faster, improves the browsing experience, but also avoids performance issues caused by loading too much content on a single page.In Anqi CMS, implementing the pagination function of the content list is flexible and direct, and it can be configured to completion in just a few steps.The Anqi CMS adopts the Django template engine syntax, allowing you to control the display logic of content through clear tags.Add pagination to your content list, we will mainly use two core tags

2025-11-09

How to customize parameter filtering for an article or product list?

In daily website operations, we often encounter such needs: we hope that users can quickly filter out the content they are interested in based on the different attributes of articles or products.For example, on a real estate website, users may want to filter for 'two bedrooms and one living room' 'school district' housing;On a product showcase website, users may want to find 'red' new T-shirts.AnQiCMS (AnQiCMS) provides a powerful and flexible custom parameter function, which can help us easily achieve this refined content filtering

2025-11-09