Reputation: 61
I have written an html-page with a textfield where text can be entered into and on the click of a button, an email containing the entered text should be sent to a predetermined emailaddress.
The page shows in Firefox and Chrome as intended, but when clicking the button, nothing happens.
function sendEmail(adress) {
Email.send("[email protected]", "[email protected]", "subject", adress, "smtp.email.com", "[email protected]", "pAssWoRd", function done(message) {
alert("Email sent successfully")
});
}
<!DOCTYPE html>
<html>
<body>
<script src="smtp.js"></script>
<form action="/action_page.php">
<p>TEXT</p>
<p>TEXT</p>
<p>TEXT</p>
Enter text here: <input type="text" name="fname"><br>
<input type=button onClick="sendEmail(fname.value);" value='send'>
</form>
</body>
</html>
The button should send the email and display the "Email sent successfully" alert-message. Can you help me in debugging this code?
Upvotes: 0
Views: 216
Reputation: 14823
We can't really debug it without the dependencies like smtp.js
, but I can teach you how to fish, I mean debug JavaScript.
Knowing how to properly debug in JS can make your life much easier.
If the tips below are too much to grab onto the first time you can always fall back to good'ol console.log
to see how your code is behaving; it's just much slower with less context & options. As others said too, you can use a classic alert
to see if code is getting where you expect it to; it can just become a pain to click through alerts if you keep refreshing and trying to debug further.
Note: It may also be possible that you're trying to use node/server side code to send an email. If that's the case then it will not work on the client side. It seems like you're trying to use a library which should allow you to use this on the client side though.
Warning: it's dangerous to pass an email server password to the client, now they'll have your credentials. This code should happen on the server side. The client side can hit an endpoint on the server that would then kick off the code to send the email from the server.
debugger;
where you want the code to break.
Email.send
.debugger;
statement.
debugger
statement. Now you will have access to the current call stack, scope/variables, as well as other useful debugging tools.
step
.step over next function call
.step into next function call
.step out of current function
. This is useful when you enter into 3rd party libraries that you have no clue what's going on and don't really care about it.Sources
tab and choose Blackbox script
.js
file.source map
that maps the minified code back to the original. Chrome will show you the original code and let you set breakpoints in it instead of trying to work with a minified version. There are still some parts of code that don't translate 100% so you may not always be able to break where you would expect.
Upvotes: 2
Reputation: 1
First things first. 1. You have an extra closing script tag 2. Make your input type submit 3. Your form action should probably be action="mailto:emailfrom ... method="post">
Upvotes: -1
Reputation: 302
Try to put alert or log to console inside function. I am able to call the function sendEmail without any issue. The only problem with your code was an extra script tag </script>
The Code:
function sendEmail(adress) {
alert("Inside sendEmail function");
Email.send("[email protected]", "[email protected]", "subject", adress, "smtp.email.com", "[email protected]", "pAssWoRd", function done(message) {
alert("Email sent successfully")
});
}
<!DOCTYPE html>
<html>
<body>
<script src="smtp.js"></script>
<form action="/action_page.php">
<p>TEXT</p>
<p>TEXT</p>
<p>TEXT</p>
Enter text here: <input type="text" name="fname"><br>
<input type=button onClick="sendEmail(fname.value);" value='send'>
</form>
</body>
</html>
Upvotes: 1