php - 如何设置网站联系表以发送电子邮件
问题描述
我在我的网站上工作,我有一个小问题我希望联系表格向我发送用户发布的信息作为电子邮件我做了一些研究,这不能用 html 或 javascript 完成,但可以用 php 完成我得到了一个一些 php 代码片段在线并放在网站上,但它不起作用,我无法真正解决它,因为我不知道 php。我需要一些帮助,我将链接放到我不介意的网站的 github 存储库如果有人查看并帮助解决方案,联系表位于 store.html 页面上,处理邮件请求的 php 文件称为 mail_handler.php 非常感谢 https://github.com/ekanime/trickbeats。混帐
解决方案
$('#contactForm').on('submit', submitForm); // step 1: connect submit action to button
function submitForm() {
// step 2: get data from input fields
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
// check if data is present
console.log(name, email, message);
// step 3: send data to contact form mailer
$.ajax({
method: 'post',
url: 'http://example.com/api/contact.php', //your API, If you do not have one, no data will sent
dataType: 'json',
data: {
'to_email': 'your.email@example.com',
'from_fname': fname,
'from_email': email,
'message': message
},
'success': formSuccess,
'error': formError
});
return false;//inorder not to referesh the page.
}
// step 4: handle response (success or error)
function formSuccess(result) {
if (result.error) { // there was an error
alert(result.error);
} else { // everything went well
alert(result.response);
$("#contactForm")[0].reset(); //instead of saying getElementById
// clear input fields
}
}
// step 5: handle error
function formError(xhr, status, error) {
alert("There has been an error. Please retry");
}
推荐阅读
- laravel - 未定义的变量?也许查询是错误的?
- html - Css 转换属性不适用于 Mozilla Firefox
- javascript - 尝试自动填充表单字段但没有运气
- .net - 无法通过 docker 连接到 SQL Server 数据库
- python - ModuleNotFoundError:即使在下载库后也没有名为“pyttsx3”的模块
- javascript - Select2插件在通过jquery动态附加新选择字段时不起作用
- javascript - 如何将多个参数解析为无状态组件?
- mongodb - 带有 eppn 的 REST-API users.list
- python-3.x - 如何在 QuerySelectField 中获取两个标签 - Flask
- r - 在ggplot中按值重新排序轴后如何更改轴文本