我已经使用emailJS.com创建了联系表格。表单正在使用以下代码:

<script type="text/javascript">

  window.onload = function () {
    document.getElementById('contact-form').addEventListener('submit', function (event) {
      event.preventDefault();
      emailjs.sendForm('gmail2', 'filtrastop', this);
      document.getElementById('contact-form').reset();
    });

    document.getElementById('form2').addEventListener('submit', function (event) {
      event.preventDefault();
      emailjs.sendForm('gmail2', 'filtrastop', this);
      document.getElementById('form2').reset();
    });
  }

</script>


我正在尝试添加确认/错误消息,该消息在提交表单时显示给用户。阅读文档后(https://www.emailjs.com/docs/sdk/send-form/

我的代码现在看起来像这样,但是我没有看到任何消息吗?请帮忙!

<script type="text/javascript">

  window.onload = function () {
    document.getElementById('contact-form').addEventListener('submit', function (event) {
      event.preventDefault();
      emailjs.sendForm('gmail2', 'filtrastop', this).then(function (response) {
        console.log('SUCCESS!', response.status, response.text);
      }, function (error) {
        console.log('FAILED...', error);
      });
      document.getElementById('contact-form').reset();
    });

    document.getElementById('form2').addEventListener('submit', function (event) {
      console.log('SUCCESS!', response.status, response.text);
    }, function (error) {
      console.log('FAILED...', error);
    });
  }

</script>

最佳答案

请检查您的“ form2” addEventListener()。如果不直接调用emailjs.send()您将直接使用响应对象,则会得到未定义的错误。您可以先使用emailjs.send()然后再使用响应对象。请检查我的代码。

我无法发表评论,因此添加了我的代码。请提供所有配置密钥的详细信息,然后运行并检查我的代码。



<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/email.min.js">
</script>
<script type="text/javascript">
    (function () {
        emailjs.init("YOUR_USER_ID");
    })();

    window.onload = function () {
        document.getElementById('contact-form').addEventListener('submit', function (event) {
            event.preventDefault();
            var templateParams = {
                to_name: 'prabhat',
                from_name: 'Tapas',
                message_html: 'Please Find out the attached file'
            };

            emailjs.send('serviceID', 'templateID', templateParams)
                .then(function (response) {
                    if (response.status == 200 && response.text == 'OK')
                        alert('Your message has been sent Successfully..!!!');
                    else
                        alert('Sorry there was a problem. Please try again...!!!');
                }, function (error) {
                    alert('Sorry there was a problem. Please try again...!!!');
                });
            document.getElementById('contact-form').reset();
        });

        document.getElementById('form2').addEventListener('submit', function (event) {
            event.preventDefault();
            var templateParams = {
                to_name: 'prabhat',
                from_name: 'Padhy',
                message_html: 'Please Find out the attached file'
            };

            emailjs.send('serviceID', 'templateID', templateParams)
                .then(function (response) {
                    if (response.status == 200 && response.text == 'OK')
                        alert('Your message has been sent Successfully..!!!');
                    else
                        alert('Sorry there was a problem. Please try again...!!!');
                }, function (error) {
                    alert('Sorry there was a problem. Please try again...!!!');
                });
            document.getElementById('form2').reset();
        });
    }
</script>


<form id="contact-form">
    <button type="submit">Click Me</button>
</form>
<form id="form2">
    <button type="submit">Click Me</button>
</form>

09-25 22:23