2017-01-10 118 views
0

我不知道我在做什么错。我正在尝试使用PHP来联系我的形式。下面是代码:使用php发送表单数据,但不发送

<?php 
if($_POST["submit"]) { 
    $recipient="[email protected]"; 
    $subject="Form to email message"; 
    $Name=$_POST["Name"]; 
    $Email=$_POST["Email"]; 
    $Reason=$_POST["Reason"]; 
    $Message=$_POST["Message"]; 

    $mailBody="Name: $Name\nEmail: $Email\n\n$Reason $Message"; 
    mail($recipient, $subject, $mailBody, "From: $Name <$Email>"); 
    $thankYou="<p>Thank you! Your message has been sent.</p>"; 
} 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <link rel="stylesheet" type="text/css" href="QDPicks.css"> 
     <title> QDPicks</title> 
    </head> 
    <body> 
     <header> 
      <a class="btn btn-primary btn-lg" href="QDPicks.html" role="button">Home</a> 
      <a class="btn btn-sample btn-lg active pull-right" href="QDPicksContactUs.html" role="button">Contact Us</a> 
      <a class="btn btn-sample btn-lg pull-right" href="QDPicksCompany.html" role="button">Company</a> 
      <a class="btn btn-sample btn-lg pull-right" href="QDPicksProducts.html" role="button">Products</a> 
     </header> 
     <header><p1>Contact Us</p1></header> 
     <form method="post" action="QDPicksContactUs.php > 
      <div class="form-group"> 
       <label for="InputReason1"></label> 
       <input type="text" class="form-control" id="InputReason1" name="Name"> 

       <label for="exampleInputEmail1"></label> 
       <input type="email" class="form-control" id="exampleInputEmail1" name="Email"> 

       <label for="InputReason1"></label> 
       <input type="text" class="form-control" id="InputReason1" name="Reason"> 
      </div> 
      <div class="form-group"> 
       <textarea type="text" class="form-control" rows="3" name="Message">  </textarea> 
       <p3 class="help-block">Explain on the reason for contact.</p3> 
      </div> 
      <div class="checkbox"> 
      </div> 
      <button type="submit" class="btn btn-default">Submit</button> 
     </form> 
     <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </script> 
    </body> 

    <!-- Latest compiled and minified CSS --> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 

    <!-- Optional theme --> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> 

    <!-- Latest compiled and minified JavaScript --> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 

</html> 

当我把我的信息并点击提交数据消失就像它已经发送了电子邮件,但没有电子邮件已经来到了也对不起 - 因为某些原因某些代码得到了削减。但它不是关键部分。

+0

你在使用localhost吗?什么是错误? –

+0

你是否检查过,当你点击提交按钮时你会得到帖子的参数? – Sona

+0

不使用记事本来编码。考虑一个IDE像PHPStorm – CodeGodie

回答

0

你忘了把行动=” QDPicksContactUs.php

这一行

<form method="post" action="QDPicksContactUs.php" > 

更换您的标签可能是这种变化后您的代码工作

0

首先在这里缺少一个双引号

<form method="post" action="QDPicksContactUs.php > 

此外,如果您在同一页面发帖,则无需在操作中提供页面名称。

像标签添加名称:

<button name="submit" type="submit" class="btn btn-default">Submit</button> 

最后用if(isset($_POST["submit"]))

而且,你在本地主机上运行的代码替换if($_POST["submit"])

+0

我有我的代码与godaddy托管,它使用cpanel –

+0

你修复工作!谢谢! –

+0

这很好。请标记为已解决。 –

0

让我们专注于发送电子邮件的代码。请阅读我的评论。

if($_POST["submit"]) { 

    // you probably shouldn't post a real email here on SO 
    $recipient="[email protected]"; 
    $subject="Form to email message"; 

    // you should validate this field if you are sticking it in your mail headers as you do below. 
    $Name=$_POST["Name"]; 

    // you should DEFINITELY validate this before trying to send mail 
    $Email=$_POST["Email"]; 
    $Reason=$_POST["Reason"]; 
    $Message=$_POST["Message"]; 

    $mailBody="Name: $Name\nEmail: $Email\n\n$Reason $Message"; 

    // first, you don't even bother checking to see if this returns TRUE 
    // secondly, because you don't validate $Name or $Email, this command is vulnerable to Mail Header Injection 
    mail($recipient, $subject, $mailBody, "From: $Name <$Email>"); 

    $thankYou="<p>Thank you! Your message has been sent.</p>"; 
} 

所以你的形式是不好的,因为你不验证什么,你不检查,看看邮件命令的实际返回一个成功的结果,这是容易Mail Header Injection

首先,验证$名称:

$Name=$_POST["Name"]; 
    if (!preg_match('/^[a-zA-Z_\-]+$/', $Name)) { 
     die("sorry! Name is not valid"); 
    } 

其次,验证$电子邮件

$Email = $_POST["Email"]; 
    if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) { 
     die("Sorry, email is not valid"); 
    } 

三,检查您的邮件功能的结果。如果它返回FALSE或其他空值,出现了问题 - 尽管如果不询问系统管理员查看邮件日志,我们可能无法找到答案。

if (!mail($recipient, $subject, $mailBody, "From: $Name <$Email>")) { 
     die("OH NO. The mail function did not work."); 
    } 

考虑阅读的manual上的邮件功能:

返回TRUE 如果邮件被成功接受交付,FALSE否则。

重要的是要注意,仅仅因为邮件被接受交付,并不意味着邮件实际上会到达预定的目的地。