嗨,我是Web开发的新手,我正尝试使用emailJS发送一个简单的联系表单,该表单一周前在我的localhost和Firebase的托管站点上正常运行,但我不知道发生了什么事正常运行,现在有时只能在Microsoft Edge浏览器中的localhost上运行。如果我尝试使用其中一个文本区域为空提交,则会弹出窗口警报,但是当我拥有所有字段时,它只会重新加载页面,而不会显示提示“已发送电子邮件!”的窗口警报 我按照本教程使用EmailJS https://blog.mailtrap.io/react-send-email/#Sending_emails_with_EmailJS 我正在使用ReactJS开发网站,所以这里是我的联系表单组件的功能,我们将为您提供任何帮助!谢谢:D
这是我处理文本区域更改的地方
constructor(props) {
super(props);
this.state ={
name: '',
email: '',
message: '',
}
this.handleChangeName = this.handleChangeName.bind(this);
this.handleChangeEmail = this.handleChangeEmail.bind(this);
this.handleChangeMessage = this.handleChangeMessage.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);;
这是我的峰会部分
handleChangeName(event) {
this.setState({name: event.target.value});
}
handleChangeEmail(event) {
this.setState({email: event.target.value});
}
handleChangeMessage(event) {;
this.setState({message: event.target.value});
}
handleSubmit(event) {
event.preventDefault();
const templateId = 'template_7HeYQshf';
if(this.state.email === '' || this.state.name === '' || this.state.message === '')
{
window.alert('All fields are required.')
window.location.reload();
}
else {
this.sendFeedback(templateId, {message_html: this.state.message, from_name: this.state.name,
reply_to: this.state.email})
}
}
sendFeedback (templateId, variables) {
window.emailjs.send(
'gmail', templateId,
variables
).then(res => {
window.alert('Email sent!');
window.location.reload();
})
// Handle errors here however you like, or use a React error boundary
.catch(err => window.alert('something went wrong, please try again '))
window.location.reload();
}
答案 0 :(得分:0)
我通过以下更改解决了该问题:
constructor(props) {
super(props);
this.state ={
name: '',
email: '',
message: '',
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
这是我的新峰会部分
handleChange = (event) => {
this.setState({[event.target.name]: event.target.value});
};
handleSubmit = (event) => {
event.preventDefault();
if(this.state.email === '' || this.state.name === '' || this.state.message === '')
{
window.alert('All fields are required.')
window.location.reload();
}
else {
emailjs.send(
"gmail",
"template_7HeYQshf",
{message_html: this.state.message, from_name: this.state.name, reply_to: this.state.email},
"*****"
)
window.alert('Email sent!')
window.location.reload();
}
}