确认消息未显示在javascript联系人表单上

我已经使用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',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',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.text);
    },function (error) {
      console.log('FAILED...',error);
    });
  }

</script>
pkhisen 回答:确认消息未显示在javascript联系人表单上

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

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

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/emailjs-com@2.4.0/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',from_name: 'Padhy',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>

,

Prabhat通过聊天非常有帮助。我们设法使其完全按照要求工作!

下面是工作代码的更新版本:

<script type="text/javascript">
(function(){
emailjs.init("user_##########");
})();
</script>
<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) {
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();
emailjs.sendForm('gmail2',function (error) {
alert('Sorry there was a problem. Please try again...!!!');
});
document.getElementById('contact-form').reset();
});
}
本文链接:https://www.f2er.com/3128750.html

大家都在问