首页 > 解决方案 > 如何设置网站联系表以发送电子邮件

问题描述

我在我的网站上工作,我有一个小问题我希望联系表格向我发送用户发布的信息作为电子邮件我做了一些研究,这不能用 html 或 javascript 完成,但可以用 php 完成我得到了一个一些 php 代码片段在线并放在网站上,但它不起作用,我无法真正解决它,因为我不知道 php。我需要一些帮助,我将链接放到我不介意的网站的 github 存储库如果有人查看并帮助解决方案,联系表位于 store.html 页面上,处理邮件请求的 php 文件称为 mail_handler.php 非常感谢 https://github.com/ekanime/trickbeats。混帐

标签: phphtmlweb

解决方案


$('#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");
}

检查您的代码,您必须将“lname”更改为“email” 在此处输入图像描述


推荐阅读