简体   繁体   English

无法发送 email 并且单击提交按钮时始终刷新

[英]Can't send email and upon clicking the submit button always refreshing

Hello Developers/Programmers, I am having a problem resolving this issue since it's my first time creating my website.你好开发人员/程序员,我在解决这个问题时遇到了问题,因为这是我第一次创建我的网站。 I want to send email from my website to my gmail and I used "submit" it's not working and it's always reloading upon clicking it.我想将 email 从我的网站发送到我的 gmail 并且我使用“提交”它不起作用并且它总是在单击它时重新加载。

Please bear with my codes I just copied some of my codes from different video tutorials and I don't understand some.请忍受我的代码我只是从不同的视频教程中复制了一些代码,我不明白一些。

     //index.html

    <script src="https://smtpjs.com/v3/smtp.js"></script>
    <script src="app.js"></script>

    <div class= "contactMe">
    <form action="" method="post">
    <h2>Send Message</h2>
    <div class = "inputBox">
    <input type= "text" class="name" name="" required= "required" placeholder="Full Name">
    </div>
 
    <div class = "inputBox">
    <input type= "text" class="email" name="" required= "required" placeholder="Your Email">
    </div>
 
    <div class = "inputBox">
    <textarea placeholder="Your message here..." class="message" required= "required"></textarea>
    </div>
 
    <div class = "inputBox">
    <input type = "submit" class="submitButton" name="" value="Send" onclick="sendEmail()">
    </div>

    </form>
    </div>
    //style.css

    .contactMe{
    position:absolute;
    bottom:20%;
    left:10%;
    width: 50%;

    display:none;


    }
    .contactMe h2{
        font-size: 30px;
        color: white;
        font-weight: 500;
        margin:20px;

      }

     }

    .contactMe .inputBox{
    position: relative;
    width: 10%;
    /*margin-top: 10px;*/
    border: 3px solid black;

    }
     .contactMe .inputBox input , textarea{

      width: 50%;
      padding: 5px 0;
      font-size: 16px;
      margin:  10px 0;
      background-color: rgba(0,0,0,0.7);
      color: white;
      border: none;
      border-bottom: 2px solid #333;
      outline: none;
      resize: none;
     }
         .contactMe .inputBox textarea{
         height: 135px;

     }
     .contactMe .inputBox input[type="submit"]{
     width: 100px;
     background: white;
     color: black;
     cursor: pointer;
     padding: 10px;
     font-size: 18px;
     }
    //app.js

    document.querySelector(".submitButton").addEventListener("submit", 
     function(e){
       e.preventDefault();
     }

     function sendEmail() {
       Email.send({
         Host : "smtp.gmail.com",
         Username : "myemail@gmail.com",
         Password : "Mypassword",
         To : 'myemail@gmail.com',
         From : "myemail@gmail.com",
         Subject : $name + "sent you a message",
         Body : "Name:" + $name <br/> "Email:"  <br/> "Message:",
        }
      ).then(message => alert("Message successfully sent"));
     }

Thank you for understanding my codes.感谢您理解我的代码。 If you know what goes wrong in my code why it's not working please tell me.如果您知道我的代码出了什么问题,为什么它不起作用,请告诉我。

The main problem is the statement, which is preventing the form to be submitted ( you call this refreshing, but it is not. when your browser opens your page for the 1st time - it makes GET and after hitting the submit button - it makes POST )主要问题是阻止提交表单的语句(您称之为刷新,但事实并非如此。当您的浏览器第一次打开您的页面时 - 它会生成GET并在点击提交按钮后 - 它会生成POST )

  1. document.querySelector(".submitButton").addEventListener("submit" is the event of the form - not of the submit button document.querySelector(".submitButton").addEventListener("submit"表单的事件——不是提交按钮的事件

  2. you have not closed the statement properly, it should look like你没有正确关闭语句,它应该看起来像

document.querySelector(".emailForm").addEventListener("submit", function(e){ e.preventDefault(); });

Another problem is the construction of subject and body of your email, you should use this Template literals with ` character instead of "另一个问题是您的email的主题和正文的构造,您应该使用带有 ` 字符而不是 "

Also, you should initialize the variable name with the value from the <input type= "text" class="name" by using var name = document.querySelector(".name").value;此外,您应该使用var name = document.querySelector(".name").value;使用<input type= "text" class="name"中的值初始化变量名称。

So the corrected snippet looks like所以更正的片段看起来像

<form action="" method="post" class="emailForm">
...
document.querySelector(".emailForm").addEventListener("submit", function(e){
   e.preventDefault();
});

function sendEmail() {
   var name = document.querySelector(".name").value;

   Email.send({
     Host : "smtp.gmail.com",
     Username : "myemail@gmail.com",
     Password : "Mypassword",
     To : 'myemail@gmail.com',
     From : "myemail@gmail.com",
     Subject : `${name} sent you a message`,
     Body : `Name: ${name} <br/> Email:  <br/> Message:`,}).then(message => alert("Message successfully sent"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击提交按钮后,我的HTML表单无法提交 - Can't get my HTML form to NOT submit upon clicking Submit button 单击提交按钮使javascript执行 - Make javascript execute upon clicking submit button 单击 EJS 中的提交按钮时无法提交表单 - can't submit a form while clicking submit button in EJS JavaScript是否可以通过单击按钮但不使用mailto:或表单操作来发送电子邮件? - Can JavaScript send an email by clicking a button but NOT using mailto: or a form action? 通过选项选择并单击提交按钮后,如何显示图像? - How can I display an image upon selecting through options and clicking submit button? 使用onclick功能单击“提交”按钮后,HTML页面正在刷新 - HTML page is refreshing after clicking on submit button with onclick function 单击提交按钮后,屏幕向下滚动,不执行任何操作 - Upon clicking the submit button the screen scrolls down and does nothing 单击HTML表单中的“提交”按钮后无法重定向 - Not able tor redirect upon clicking on submit button in HTML form 单击复选框并单击“提交”按钮javascript时切换“隐藏/显示” - Toggle Hide/show upon clicking a checkbox and hitting submit button javascript 单击“提交”按钮后如何在网页上显示文本 - How do I display text on a webpage upon clicking a 'Submit' button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM