I have solved my issue by doing the following:
Instead of “rejecting” an input which contains an ampersand (&) I have used the code below to change any instance of “&” to “and” before a user submits the form.
Example below.
$w.onReady(function () {
$w('#input2').onFocus((event)=>{
let str = $w('#input1').value;
$w('#input1').value = str.replace(/&/g,'and');
});
});
Form Title
[ input1 ] **will change any instance of “&” to “and” once user focuses on Input2)
[ input2 ]
[ Submit Button ]
Original
Hi guys,
I set up a few custom forms using input fields. When a user fills in a form a copy of the submission is emailed to both the user and I.
The issue:
When a user inputs an ampersand “&” the email triggered when they click submit is incomplete. (i.e. any info input after the ampersand is cut off.)
I see the full info logged in the dataset. So I know Wix has logged it correctly. It just doesn’t email correctly. I’m assuming & is the issue. Is there a work-around?
Form ex. https://www.otodatatankmonitors.com/call-back
Thank you for your help,
B
import {sendEmail, sendEmailWithRecipient} from 'backend/email';
$w.onReady(function () {
$w("#dataset1").onAfterSave(sendFormData);
});
function sendFormData() {
const subject = `Thank You For Your Request, ${$w("#input3").value}`;
const body = `Hello, ${$w("#input3").value}. So, you're interested in testing our product free of charge? We're happy to hear that! The sales team will be in touch soon. In the meantime, please review the contact information you provided below:
\rName: ${$w("#input3").value}
\rCompany: ${$w("#input6").value}
\rPhone Number: ${$w("#input4").value}
\rEmail: ${$w("#input5").value}
\rCity: ${$w("#input8").value}
\rState or Province: ${$w("#input7").value}
\rNotes: ${$w("#textBox1").value}
If all the above information is correct, no action is required from you.`;
const recipient = $w("#input5").value;
sendEmailWithRecipient(subject, body, recipient)
.then(response => console.log(response));
sendEmail(subject, body)
.then(response => console.log(response));
}
---
//sendGrid.js
import {fetch} from 'wix-fetch';
export function sendWithService(key, sender, recipient, subject, body) {
const url = "https://api.sendgrid.com/api/mail.send.json";
const headers = {
"Authorization": "Bearer " + key,
"Content-Type": "application/x-www-form-urlencoded"
};
const data = `from=${sender}&to=${recipient}&subject=${subject}&text=${body}`;
const request = {
"method": "post",
"headers": headers,
"body": data
};
return fetch(url, request)
.then(response => response.json());
}
---
///email.jsw
import {sendWithService} from 'backend/sendGrid';
export function sendEmail(subject, body) {
const key = "Hidden";
const sender = "marketing@myemail.com";
const recipient = "marketing@myemail.com";
return sendWithService(key, sender, recipient, subject, body);
}
export function sendEmailWithRecipient(subject, body, recipient) {
const key = "Hidden";
const sender = "marketing@myemail.com";
return sendWithService(key, sender, recipient, subject, body);
}