douxuelv7755 2018-05-14 15:59
浏览 88
已采纳

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

I have been working on a form in a php file. As you'll see from the code below I have written everything in a PHP file including the form tag. This form.php file is being called from another source in my folder called contact.php. I'll admit this is the neatest written code. But for some reason when I submit my information on this form, it tells me that the form has been submitted. Yet I do not receive an email ($email_to) to my billy.farroll@hotmail email address with any of the submitted information. It also works with the google recaptcha and if there is anything invalid entered. Just for some reason it will not send through the data to me i.e. Name, Email, Contact Number or the Enquiry.

Help would be GREATLY appreciated.

My Inline CSS style:

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

My 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 
";
  "Reply-To: $email_from 
";
  $message =
  "Name: ". $name .
  "
Mobile Number: " . $telephone .
  "
Email Address: " . $email .
  "


" .
  "

Message: 
" . $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条回答 默认 最新

  • dongxueji2838 2018-05-14 16:08
    关注

    Your problem is most probably in the <form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post" style="text-align:left;">

    you can:

    1. completely remove the action

    2. at least surround the <?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> with double quotes ""

    3. use action=""

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败