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

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏