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

    点赞 打赏 评论
  • dpb_4431 2018-05-15 08:35

    I found what the problem was, after talking to the the ISP they told me there were slight changes to the authorization of the server. In that, the email address that sends the information has to be verifed on the server i.e. $email_from ="billy.farroll@hotmail.com" -- has to be a verifed email address so that it has the authorization to send the data through to wherever you'd like it to be sent to i.e. $email_to = "billy.farroll@hotmail.com" Also, in the $sent variable the $email had to change to $email_from (due to the authorization situation). See code below, is working successfully now:

    OLD CODE:

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

    NEW CODE:

    $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 
    "; // 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 
    ";  // 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 .
      "
    Mobile Number: " . $telephone .
      "
    Email Address: " . $email .
      "
    
    
    " .
      "
    
    Message: 
    " . $question;
      ini_set("sendmail_from", $email_from);
      $sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
      if ($sent)   
    
    点赞 打赏 评论

相关推荐 更多相似问题