电子邮件表格不起作用

时间:2014-05-16 12:26:20

标签: php html ajax forms contact

我在bootstrap上创建了一个网站。通过使用Ajax和PHP,将表单链接到它。表单不会发送任何电子邮件。我把它放在服务器上检查它。有人可以帮我解决一下代码。

<form name="contactform" method="post" action="" >
                    <div class="form-group">

                        <input type="email" class="form-control" name="email" id="email" placeholder="Enter email" style="width:80%; height: 40px; font-size: 16px; ">
                    </div>
                    <div class="form-group">

                        <input type="password" class="form-control" name="password" id="password" placeholder="Password" style="width:80%; height: 40px; font-size: 16px;">
                    </div>

                    <button type="submit" onclick="myFunction()" class="btn btn-default" style="width: 80%; height: 55px; font-size: 17px; color: white; border-radius: 11px; border: none; background: #F59E3B"><span class="glyphicon glyphicon-play" style="color: white; float: left;"></span>Yes, get me RTD now!</button>
                </form>
                <div id="myDiv"></div>

这是AJAX代码:

<script type="text/javascript">
function myFunction()
{


    var xmlhttp;
    if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
  var email=document.getElementById("email").value;
  var password=document.getElementById("password").value;
  xmlhttp.open("POST","senderemail.php?email="+email+"&password="+password,true);
  xmlhttp.send();
}
</script>

这是PHP代码:

    if(isset($_REQUEST["email"]))
{

 $email_to="xxxxx@example.com";
 $email_subject="RTD enquiry ";
 echo $email_from=$_REQUEST["email"];
 $email_message="NEW User:".$email_from;
 echo $password="Password: ". $_REQUEST["password"];

  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {

      echo  'OOPS !! It seems you have entered a wrong Email ID.<br />';

  }
  else
  {
   $headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n" .'X-Mailer: PHP/' . phpversion();
     if(@mail($email_to, $email_subject, $email_message, $headers))
     {
        echo "<div class='thanks'><span id='Nihal'>Many thanks for getting in touch.</span><span class='sub-header'>We'll get back to you as soon as we can.</span></div>";
     }
     else
     { 
       echo "Sorry :( Please try again later";
     }
  }

}

2 个答案:

答案 0 :(得分:1)

假设没有其他错误,那么此代码应该适合您。

如果您使用POST发送数据,则无法使用网址传递查询参数。 你必须单独发送。

xmlhttp.open("POST","senderemail.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("email="+email+"&password="+password);

答案 1 :(得分:0)

使用POST请求时,请勿将参数放入URL中。 但是,使用帖子请求很好。 在通话xmlhttp.send();中添加参数 即xmlhttp.send("email="+email+"&password="+password);