Form.php文件未通过输入数据发送

时间:2018-05-14 15:59:22

标签: php html forms

我一直在处理php文件中的表单。正如您从下面的代码中看到的,我已经在包含表单标签的PHP文件中编写了所有内容。这个form.php文件是从我名为contact.php的文件夹中的另一个源调用的。我承认这是最好的书面代码。但由于某些原因,当我在此表单上提交我的信息时,它告诉我表单已提交。但我没有收到我的billy.farroll@hotmail电子邮件地址的电子邮件($ email_to)以及任何提交的信息。它也可以与google recaptcha一起使用,如果输入了任何无效的内容。由于某种原因,它不会通过数据发送给我,即姓名,电子邮件,联系电话或查询。

非常感谢帮助。

我的内联CSS样式:

    <style>
    form span{
    color: red;
      }
    form input[type="submit"]{
    background: url('images/logo2.png') no-repeat;
    background-size: contain;
    overflow: visible;
    padding-left: 45px;
    height: 20px;
    border: 0px;
    margin: 0 auto;
    margin-top: 5px;
    font-family: 'Orbitron', sans-serif;
    text-transform: uppercase;
      }
    .g-recaptcha {
    margin: 5px;
      }
   </style>

我的PHP

    <?php
    if(strpos($_SERVER['REQUEST_URI'], 'form' )!== false){
    header('Location:index.php');
    }
    if($_POST){
    $email;
    $name;
    $captcha;
    $telephone;
    $question;
    if(isset($_POST['email']))
      $email=$_POST['email'];
    if(isset($_POST['name']))
      $name=$_POST['name'];
    if(isset($_POST['telephone']))
      $telephone=$_POST['telephone'];
    if(isset($_POST['message']))
      $question=$_POST['message'];
    if(isset($_POST['g-recaptcha-response']))
      $captcha=$_POST['g-recaptcha-response'];


    $email = htmlspecialchars($_POST['email']);
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) || ($name == "") || 
    ($question == "") ||(!$captcha))
    {
      echo "<p>Invalid Input.</p>";
      ?>
      <form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> 
      method="post" style="text-align:left;">
        <label>Name: <span>*</span></label><input name="name" type="text" />
        <label>Email: <span>*</span></label><input name="email" type="text" />
        <label>Contact Number: </label><input name="telephone" type="text" />
        <label>Enquiry: <span>*</span></label>
        <textarea name="message" rows="10" cols="30"></textarea>
        <div class="g-recaptcha" data-sitekey="<!-- SITEKEY -->"></div>
        <input type="submit" value="submit" />
       </form>
       <?php
       } else {



  $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=<!-- SECRET KEY -->=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
    if($response['success'] == false)
    {
      echo '<h2>Spam detected</h2>';
    }

    else   {



  $email_from = $_POST["email"];
  $email_to = "billy.farroll@hotmail.com";
  $question = $_POST["message"];
  $email_subject = "Enquiry";
  $headers =
  "From: $email_from \n";
  "Reply-To: $email_from \n";
  $message =
  "Name: ". $name .
  "\r\nMobile Number: " . $telephone .
  "\r\nEmail Address: " . $email .
  "\r\n\r\n\r\n" .
  "\r\n\r\nMessage: \r\n" . $question;
  ini_set("sendmail_from", $email_from);
  $sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);
  if ($sent)
  {
    echo 'Thank you for your enquiry, one of our team will be in contact with you shortly.';
  }

    }
}

} else {
  ?>
  <form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post" style="text-align:left;">
    <label>Name: <span>*</span></label><input name="name" type="text" />
    <label>Email: <span>*</span></label><input name="email" type="text" />
    <label>Contact Number: </label><input name="telephone" type="text" />
    <label>Enquiry: <span>*</span></label>
    <textarea name="message" rows="10" cols="30"></textarea>
    <div class="g-recaptcha" data-sitekey="<!-- SITEKEY -->"></div>
    <input type="submit" value="submit" /><span> * required fields</span>
  </form>


  <?php } ?>

2 个答案:

答案 0 :(得分:3)

您的问题很可能出现在<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post" style="text-align:left;">

中 你可以:

  1. 完全删除操作

  2. 至少使用双引号<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>围绕""

  3. 使用action=""

答案 1 :(得分:1)

我发现问题是什么,在与ISP交谈后,他们告诉我服务器的授权略有变化。在那里,发送信息的电子邮件地址必须在服务器上验证,即$ email_from =“billy.farroll@hotmail.com” - 必须是一个经过验证的电子邮件地址,以便它有权通过服务器发送数据到你想要发送到的任何地方,即$ email_to =“billy.farroll@hotmail.com”此外,在$ sent变量中,$ email必须更改为$ email_from(由于授权情况)。请参阅下面的代码,现在正在成功运行:

旧代码:

 $email_from = $_POST["email"];
  $email_to = "billy.farroll@hotmail.com";
  $question = $_POST["message"];
  $email_subject = "Enquiry";
  $headers =
  "From: $email_from \n";
  "Reply-To: $email_from \n";
  $message =
  "Name: ". $name .
  "\r\nMobile Number: " . $telephone .
  "\r\nEmail Address: " . $email .
  "\r\n\r\n\r\n" .
  "\r\n\r\nMessage: \r\n" . $question;
  ini_set("sendmail_from", $email_from);
  $sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);
  if ($sent)

新代码:

$email_from = "billy.farroll@hotmail.com"; // This email address has to be the same email on the server if using Fasthots server i.e. strawberry server - billy@strawberrymarketing.com - SENDS THE EMAIL
  $email_to = "billy.farroll@hotmail.com"; // Where the email is being sent to
  $question = $_POST["message"];
  $email_subject = "Enquiry";
  $headers =
  "From: $email \n"; // This is what's shown in the receiving message mail the email variable is essential here because it's whats entered by user
  "Reply-To: $email \n";  // This is what's shown in the receiving message mail the email variable is essential here because it's whats entered by user
  $message =
  "Name: ". $name .
  "\r\nMobile Number: " . $telephone .
  "\r\nEmail Address: " . $email .
  "\r\n\r\n\r\n" .
  "\r\n\r\nMessage: \r\n" . $question;
  ini_set("sendmail_from", $email_from);
  $sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
  if ($sent)