【发布时间】:2021-07-25 14:16:18
【问题描述】:
我正在使用 Formsubmit 将联系我们表单发送到我的电子邮件,到目前为止它正在工作。
但我想当用户发送表单时它不会将其重定向到源thank you page,而是我想在同一页面上显示感谢文本。我想用我自己的。
所以我关注了documentation,我想出了以下内容: 不幸的是,当我尝试验证表单按钮时它不起作用
<form id="contactForm" action="https://formsubmit.co/el/myemail" method="POST">
<input type="text" name="name">
<input type="phone" name="phone" inputmode="tel">
<input type="hidden" name="_subject" value="Partenaires">
<input type="email" name="email" >
<input type="text" name="message" >
<button class="btn btn-primary border rounded-0 shadow-lg d-lg-flex justify-content-lg-center"
type="submit" name="sendData">Send</button>
<!-- <input type="hidden" name="_next" value="thanks.html">-->
<script type="text/javascript">
var frm = $('#contactForm');
var msg_res ='';
frm.submit(function (e) {
e.preventDefault();
$.ajax({
type: frm.attr('method'),
method: "POST",
url: "https://formsubmit.co/el/vupawa",
dataType: 'html',
accepts: 'application/json',
data: frm.serialize(),
success: function (response) {
$("#message").html(response);
if(response != msg_res){
msg_res = response; //store new response
alert('New message received');
}
},
error: function (response) {
console.log('An error occurred.');
console.log(response);
}
complete: function(response){
setTimeout(ajax,1000);
}
});
});
});
</script>
注意:取消注释这一行 <input type="hidden" name="_next" value="thanks.html"> 将验证表单并通过另一个页面,这不是我想要的。
有什么想法吗?
【问题讨论】:
-
从表单中移除 action 属性
-
在成功函数中添加提醒功能
-
没用我相信action属性是必需的,当我在本地尝试时我确实得到了警报成功但是当我在主机服务器上尝试时我更新了
标签: javascript html ajax form-submit