dou2347 2013-05-23 18:41 采纳率: 0%
浏览 1212
已采纳

我似乎无法让我的表单成功发送电子邮件

This is my first time creating a website combining AJAX, JavaScript, PHP, and HTML. Basically, I have a modal that pops up when you click "contact me" with a form inside. The reason I've chosen to use AJAX is because I don't want the page to refresh or remove the modal. I just want it to say something simple afterwards inside the modal.

My problem is, I don't receive the email from the form once I hit submit, and when I trace the emails sent to my address from my server, they don't even show up. This isn't my first time building a web form or using PHP/HTML, so I'm convinced the problem has to be with the AJAX.

So, here is what I have, and I can't seem to figure out why it won't work...

PHP:

<?php
$email_to="conrad.h.appel.iv@gmail.com";
$email_subject="Form Submission";

$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];

function clean_string($string)
{
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($name)."
";
$email_message .= "E-Mail Address: ".clean_string($email)."
";
$email_message .= "Message: ".clean_string($message);

$headers='From: '.$email."
".'Reply-To: '.$email."
".'X-Mailer: PHP/'.phpversion();
@mail($email_to,$email_subject,$email_message,$headers);
die();
?>

Javascript/AJAX:

function submitForm() {
    var request;
    var name = $('#name').val();
    var email = $('#email').val();
    var message = $('#message').val();
    console.log(name);
    console.log(email);
    console.log(message);

    var the_data = "name="+name+"&email="+email+"&message="+message;

    if(window.XMLHttpRequest)
    {
        request = new XMLHttpRequest();
    } else {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    /* My attempt to try some jQuery.... Didn't work either
    $.ajax({
        type: "POST",
        url: "formsubmit.php",
        data: { name: $('#name'), email: $('#email'), message: ('#message') }           
            })*/

    request.open("POST","formsubmit.php", true);
    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    request.send(the_data);
    $('#contactform').fadeOut();
    $('#contactsuccess').fadeIn();
}

Also the submit button, just in case that is an issue...

<button type="submit" class="btn btn-primary" onClick="submitForm()"><i class="icon-ok icon-white"></i> Submit!</button>

Edit: Also, I'm having trouble with this same code in that it refreshes the page, the exact thing I didn't want it to do. Due to this, I can't see my Javascript console or anything. I assumed AJAX would remove the refresh but it hasn't. How do I fix this as well?

  • 写回答

1条回答 默认 最新

  • dongzhui2636 2013-05-23 20:24
    关注

    As you're using jQuery for fetching inputs values, use $.ajax() instead of XMLHttpRequest. Your first approach (now commented) didn't work because you were using the object instead of the value ( $('#name') vs $('#name').val() ).

    The page will reload because there is no return value. Use

    <button type="submit" class="btn btn-primary" onClick="return submitForm();"><i class="icon-ok icon-white"></i> Submit!</button>
    

    And also add return false; at the end of your function.

    To see if there is a problem with your PHP script, I suggest the following:

    1. Add error_reporting(E_ALL); at the beginning of your file
    2. Remove @ of your mail function
    3. In your JS function:

      $.ajax({
          [...]
          success: function(msg) {
              alert(msg);
          }
      });
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建