duanou2016 2015-10-19 04:55
浏览 29

提交表单后不会出现成功/失败消息

I got this code somewhere online and it works except for the success and failure message. When I submit the form it just goes to a blank page. How can I show the message whether it is sent or not? Please help and Thanks in advance! :)

HTML code:

<form class="form-horizontal" role="form" method="post" action="contact.php" style=" padding:30px;">
      <div class="form-group">
        <label for="name" class="col-sm-3 control-label">Name</label>
        <div class="col-sm-6">
          <input type="text" class="form-control" id="name" name="name" placeholder="First and Last Name">
          <?php echo $errName; ?>
        </div>
      </div>
      <div class="form-group">
        <label for="email" class="col-sm-3 control-label">Email</label>
        <div class="col-sm-6">
          <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com">
          <?php echo $errEmail; ?>
        </div>
      </div>
      <div class="form-group">
        <label for="message" class="col-sm-3 control-label">Message</label>
        <div class="col-sm-6">
          <textarea class="form-control" rows="4" name="message"></textarea><?php echo $errMessage; ?>

        </div>
      </div>

      <div class="form-group">
        <div class="col-sm-6 col-sm-offset-3">
          <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-6 col-sm-offset-3">
          <?php echo $result; ?>  
        </div>
      </div>
    </form> 

and here is my PHP code

    <?php
  if ($_POST["submit"]) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = intval($_POST['human']);
    $from = 'Contact Form'; 
    $to = 'nikita_lim@rocketmail.com'; 
    $subject = 'New Message ';

    $success = mail($to, $subject, $message);

    $body ="From: $name
 E-Mail: $email
 Message:
 $message";
    // Check if name has been entered
    if (!$_POST['name']) {
      $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
      $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
      $errMessage = 'Please enter your message';
    }
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
  if (mail ($to, $subject, $body, $from)) {
    $result='<div class="alert alert-success">Thank you for contacting us. We will be in touch with you very soon.
</div>';
  } else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
  }
}
  }
?>
  • 写回答

2条回答 默认 最新

  • doujiyan0971 2015-10-19 05:08
    关注

    Try this PHP code

    <?php
          if ($_POST["submit"]) {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $message = $_POST['message'];
            //$human = intval($_POST['human']);
            $from = 'Contact Form'; 
            $to = 'nikita_lim@rocketmail.com'; 
            $subject = 'New Message ';
    
            //$success = mail($to, $subject, $message);
    
            $body ="From: $name
     E-Mail: $email
     Message:
     $message";
            // Check if name has been entered
            if (!$_POST['name']) {
              $errName = 'Please enter your name';
            }
    
            // Check if email has been entered and is valid
            if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
              $errEmail = 'Please enter a valid email address';
            }
    
            //Check if message has been entered
            if (!$_POST['message']) {
              $errMessage = 'Please enter your message';
            }
        // If there are no errors, send the email
        if (!isset($errName) && !isset($errEmail) && !isset($errMessage)) {
          if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank you for contacting us. We will be in touch with you very soon.
        </div>';
          } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
          }
        }
        else{
            $result='<div class="alert alert-danger">Please enter valid details.</div>';
            }
          }
    
        ?>
    

    Please use isset or empty before print php variable

    评论

报告相同问题?