【问题标题】:Form.php file not sending through inputted dataForm.php 文件未通过输入数据发送
【发布时间】:2018-05-14 15:59:22
【问题描述】:

我一直在处理 php 文件中的表单。正如您将在下面的代码中看到的那样,我已经将所有内容都写在了一个 PHP 文件中,包括表单标签。这个 form.php 文件是从我的文件夹中名为contact.php 的另一个源调用的。我承认这是最整洁的书面代码。但是由于某种原因,当我在此表单上提交我的信息时,它告诉我该表单已提交。然而,我的 billy.farroll@hotmail 电子邮件地址没有收到包含任何提交信息的电子邮件 ($email_to)。它也适用于谷歌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 } ?>

【问题讨论】:

  • 嗯,试试action=""
  • 如果您完全删除action = "",表单将默认操作为$_SERVER["PHP_SELF"]
  • 我要么用双引号将&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?&gt; 括起来,要么完全去掉它。没有任何操作的表单将始终回发给自身。如果您仍然遇到问题,请尝试将 var_dump($_POST) 添加到您的 PHP 脚本中,看看您是否收到任何发布的内容。
  • 已经尝试完全删除操作并添加双引号,即 action="" 都不起作用,还有其他解决方案吗?请。

标签: php html forms


【解决方案1】:

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

你可以:

  1. 完全删除操作

  2. 至少用双引号将&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?&gt; 括起来""

  3. 使用action=""

【讨论】:

  • 已经尝试完全删除操作并添加双引号,即 action="" 都不起作用,还有其他解决方案吗?请。
【解决方案2】:

我发现问题出在哪里,在与 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)   

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 2015-04-02
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 2015-08-31
    • 2021-04-11
    相关资源
    最近更新 更多