dsh1956 2014-07-11 07:38
浏览 81

将数据发送到php然后通过电子邮件发送

I'm having issues trying to get my contact form to email me all the entered form values. Currently the email I'm receiving is only displaying the "message" value, not the name, phone or email. I've looked at a few tutorials but I need something that doesn't reload the page so I'm giving ajax a go. I'm not strong with php or javascript at all so I'm probably missing something pretty obvious..

The alert that is commented out in the script does work.

This is my script:

$('#myform').on('valid.fndtn.abide', function() {
    var Name = $("input#Name").val();
    var Email = $("input#Email").val();
    var Phone = $("input#Phone").val();
    var Message = $("textarea#Message").val();
    var dataString = 'name='+ Name + '&email=' + Email + '&phone=' + Phone + '&Message=' + Message;
    //alert (dataString);return false;
    $.ajax({
        type: "POST",
        url: "contactengine.php",
        data: dataString,
        success: function() {
        $('#contact_form').html("<div id='success-message'></div>");
        $('#success-message').html("<h2>Message sent!</h2>")
            .append("<p>We will be in touch soon. If you haven't had a reply within 2 working days please check your junk folder and mark us as safe.</p>")
             .hide()
             .fadeIn(1500, function() {
                 $('#success-message');
                  });
                }
              });
              return false;

              });

And my php:

        <?php

    $EmailFrom = "me@myemail.co.nz";
    $EmailTo = "me@myemail.co.nz";
    $Subject = "New message from website contact form";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Phone = Trim(stripslashes($_POST['Phone'])); 
    $Email = Trim(stripslashes($_POST['Email'])); 
    $Message = Trim(stripslashes($_POST['Message'])); 

    // validation
    $validationOK=true;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "
";
    $Body .= "Phone: ";
    $Body .= $Phone;
    $Body .= "
";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "
";
    $Body .= "Message: ";
    $Body .= "
";
    $Body .= $Message;
    $Body .= "
";

    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用