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

时间:2015-10-19 04:55:20

标签: php forms submit

我在网上的某个地方获得了这个代码,除了成功和失败消息之外它都有效。当我提交表单时,它只是一个空白页面。如何显示消息是否已发送?请提前帮助和谢谢! :)

HTML代码:

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

这是我的PHP代码

    <?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\n E-Mail: $email\n Message:\n $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 个答案:

答案 0 :(得分:0)

试试这个<?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\n E-Mail: $email\n Message:\n $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>'; } } ?> 代码

- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
    tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}

请在打印php变量之前使用 isset 清空

答案 1 :(得分:0)

$result='Sorry there was an error sending your message. Please try again later.';
echo "<script type='text/javascript'>alert('$result');</script>";