简体   繁体   English

电子邮件表格无效

[英]Email form doesn't work

I made a website on bootstrap. 我在bootstrap上做了一个网站。 And by using Ajax and PHP, linked a form to it. 并通过使用Ajax和PHP将表单链接到该表单。 The form doesn't send any emails. 该表格不发送任何电子邮件。 I have put it on the server to check it as well. 我也将其放在服务器上进行检查。 Can someone please help me out with the code. 有人可以帮我解决代码问题吗?

<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>

And this is the AJAX code: 这是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>

And here is the PHP code: 这是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";
     }
  }

}

Assuming that there are no other errors than this code should work for you. 假定没有其他错误,但此代码对您来说应该有用。

If You are sending data with POST then you can't pass query parameters with url. 如果要通过POST发送数据,则不能使用url传递查询参数。 You have to send it separately. 您必须单独发送。

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

Do not put the parameters into the URL when using a POST request. 使用POST请求时,请勿将参数放入URL中。 Using a post request is fine, though. 不过,使用发布请求是可以的。 Add the parameters in the call xmlhttp.send(); 在调用xmlhttp.send();添加参数xmlhttp.send(); ie xmlhttp.send("email="+email+"&password="+password); xmlhttp.send("email="+email+"&password="+password);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM