【问题标题】:Email form doesn't work电子邮件表格不起作用
【发布时间】:2014-07-04 21:32:25
【问题描述】:

我在 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";
     }
  }

}

【问题讨论】:

  • 你有邮件服务器吗?
  • 专业提示:调试时,不要在函数声明前使用@,因为您正在抑制由所述函数生成的任何错误报告。 (这里可能不是这种情况,但仍然是一种可怕的做法)
  • 那么你得到Sorry :( Please try again later作为输出了吗?
  • @Ohgodwhy 好的朋友。会记住这一点。
  • 点击按钮后页面是否刷新?因为我没有看到任何阻止表单提交的默认操作的代码

标签: php html ajax forms contact


【解决方案1】:

假设除了此代码之外没有其他错误应该适合您。

如果您使用POST 发送数据,那么您不能使用url 传递查询参数。 您必须单独发送。

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

【讨论】:

    【解决方案2】:

    使用 POST 请求时不要将参数放入 URL。 不过,使用发布请求很好。 在调用xmlhttp.send();中添加参数 即xmlhttp.send("email="+email+"&amp;password="+password);

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 2017-10-08
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2016-12-14
      • 2015-11-03
      • 1970-01-01
      相关资源
      最近更新 更多