Leave a message verification code using tags

When you need to enable the verification code in the website message or comment, you can handle it as follows:

  1. Enable the background comment captcha function

yanzhengma1

  1. Add captcha field and code to the comment or review form:
<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>
    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>
</div>

If your website uses jQuery,The part can use jQuery syntax:

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

At this point, your message or comment has enabled the verification code verification function, and users need to enter the verification code to verify when leaving a message.