I have a form that triggers an email when submitted. At one point, I didn’t get an email because the submitter didn’t enter an email address. Rather than making it a required field, I tried to assign a standard value if the email field is empty. I can’t figure out why my if statement does not work. When I try out the code, I get an error that the ‘sender’ variable is undefined.
Here is the code:
function sendFormData() {
const subject = `New Adoption Application - ${$w("#fullnameInput").value}`;
const body = `Name: ${$w("#fullnameInput").value}
\rEmail: ${$w("#emailInput").value}
\rDog: ${$w("#preferreddogText").value}
\rAddress: ${$w("#fulladdressInput").value}
\rPhone: ${$w("#primaryphoneInput").value}`;
if ($w("#emailInput") === "") {
const sender = $w("#emailInput").value;
} else {
const sender = "donotreply@gmail.com";
}
sendEmail(subject, body, sender)
.then(response => console.log(response));
}
Everything works fine if I just assign a value to sender. I have tried several iterations in the if statement
if($w{"#emailInput"}.value === "")
I tried
if($w{"#emailInput"}.valid)
I tried null rather than “”…more than that,
I don’t understand why it doesn’t jump to the ‘else’ part and just assign a value…it does not assign a value to ‘sender’ at all.
One thing I noted in the developer tools is that the inside the if / else statement, the first ‘sender’ was shown as _sender and the second statement (in the else part) was shown as _sender2.
It appears that you have an equal number of parentheses → ‘(’ and ‘)’ (13) and that you have an equal number of curly braces → ‘{’ and ‘}’ (9) so I don’t believe that’s the issue.
I’m thinking that your issue may be resolved by replacing the following line:
if ($w("#emailInput") === "") {
with the following line:
if ($w("#emailInput").value === "") {
$w(“#emailInput”) represents an object whereas $w(“#emailInput”).value is the property of the object (which is what you really want to check/compare against).
Thanks for the input. Neither suggestion made a difference though…
Const plays by different rules. Try using let instead. The suggestion to use the value on the input should be retained.
let sender = “”;
if ($w(“#emailInput”).value !== “”) {
sender = $w(“#emailInput”).value;
} else {
sender = “donotreply@gmail.com”;
}
@tony-brunsman Thank you very much! That did it. I wasn’t aware I could use let for this. I will have to look at the difference.
Just curious; would the following have also worked:
const sender = "";
if ($w("#emailInput").value !== "") {
sender = $w("#emailInput").value;
} else {
sender = "donotreply@gmail.com";
}
?
The idea is the same as defining the let before the if so that it’s not scoped to within the if but to within the sendFormData() function.
One of the different rules that const plays by is that it cannot be reassigned. The above will result in an error. In effect, once a variable declared with const is assigned, they become read-only. Let doesn’t have that restriction. You will find a thorough explanation here .