Bootstrap PHP表单未发送

I am trying to create a very simple contact form for my personal website. This form worked perfect for another website I did so I'm not sure what the problem is here. I've spent the last few hours trying to figure it out and dipping into other form tutorials so its strayed from the original code. I guess the biggest difference between this and the other website is that this is in bootstrap.

I have three problems with this.

1) Is there anyway not to jump up to the top of the screen when I submit the form? I believe this has something for making the action the page itself.

2) Is there any way to not need the 'subject' variable for the mail function? I'd love to not have the subject input on my form.

3) The biggest problem is that while the form runs, I have not received any emails from the form.

The php before html on same doc:

if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form'; 
$to = 't*************@gmail.com'; 
$subject = $_POST['subject'];

$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';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
  $errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in    
touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error   
sending your message. Please try again later.</div>';
}
}
}
?>

The html/form:

 <form class="form-horizontal" role="form" method="post"
 action="pauline.php">
 <div class="form-group">
 <label for="name" class="col-sm-2 control-label">Name</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="name" name="name" 
 placeholder="First & Last Name" value="<?php echo  
 htmlspecialchars($_POST['name']); ?>">
      <?php echo "<p class='text-danger'>$errName</p>";?>
    </div>
  </div>
  <div class="form-group">
    <label for="email" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="email" 
name="email" placeholder="example@domain.com" value="<?php echo   
htmlspecialchars($_POST['email']); ?>">
      <?php echo "<p class='text-danger'>$errEmail</p>";?>
    </div>
  </div>
  <div class="form-group">
    <label for="name" class="col-sm-2 control-
label">Subject</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="subject" 
name="subject" placeholder="Message Subject" value="<?php echo 
htmlspecialchars($_POST['subject']); ?>">
      <?php echo "<p class='text-danger'>$errSubject</p>";?>
    </div>
  </div>
  <div class="form-group">
    <label for="message" class="col-sm-2 control-
label">Message</label>
    <div class="col-sm-10">
      <textarea class="form-control" rows="4" name="message"><?php 
 echo htmlspecialchars($_POST['message']);?></textarea>
      <?php echo "<p class='text-danger'>$errMessage</p>";?>
    </div>
  </div>
  <div class="form-group">
    <label for="human" class="col-sm-2 control-label">2 + 3 = ?                
 </label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="human" 
 name="human" placeholder="Your Answer">
      <?php echo "<p class='text-danger'>$errHuman</p>";?>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-10 col-sm-offset-2">
      <input id="submit" name="submit" type="submit" value="Send" 
  class="btn btn-primary">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-10 col-sm-offset-2">
      <?php echo $result; ?>  
    </div>
  </div>
  </form> 

I have your issues resolved with example code. See code comments for exact edits

  1. This is just a matter of setting an anchor to jump the back page down on load
  2. Just set the subject to a blank, and not have any html for the subject
  3. This was fixed on its own by cleaning up your undefined errors. Note the issets() I used with shorthand if statements $if?$then:$else to define the variables as blank if there is nothing coming from $_POST

I recommend you take care of your format needs on top to keep your code as organized as you can. Also use the variables you defined as opposed to calling $_POST repeatedly like your example.

 <?

    $name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ""; ### shorthand if statements
    $email = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : "";
    $message = isset($_POST['message']) ? htmlspecialchars($_POST['message']) : "";
    $human = isset($_POST['human']) ? intval($_POST['human']) : -1;
    $from = 'Contact Form';
    $to = 't*************@gmail.com';
    $subject = ""; ### send a blank subject. issue 2 solved
    $body = "From: $name
 E-Mail: $email
 Message:
 $message";
    $errName = $errEmail = $errMessage = $errHuman = $result = ""; ### defining these all as blank to stop the undefined error.

    if (isset($_POST["submit"])) {

        // Check if name has been entered
        if (!$name) {
            $errName = 'Please enter your name';
        }

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

        //Check if message has been entered
        if (!$message) {
            $errMessage = 'Please enter your message';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam is incorrect';
        }
        // If there are no errors, send the email
        if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
            if (mail($to, $subject, $body, $from)) {
                $result = '<div class="alert alert-success">Thank You! I will be in
            touch</div>';
            }
            else {
                $result = '<div class="alert alert-danger">Sorry there was an error
            sending your message. Please try again later.</div>';
            }
        }
    }
?>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form class="form-horizontal" role="form" method="post"
      action="pauline.php#submit"> <!-- // this solves issue 1 -->
    <div class="form-group">
        <label for="name" class="col-sm-2 control-label">Name</label>

        <div class="col-sm-10">
            <input type="text" class="form-control" id="name" name="name"
                   placeholder="First & Last Name" value="<?php echo $name; ?>">
            <?php echo "<p class='text-danger'>$errName</p>"; ?>
        </div>
    </div>
    <div class="form-group">
        <label for="email" class="col-sm-2 control-label">Email</label>

        <div class="col-sm-10">
            <input type="email" class="form-control" id="email"
                   name="email" placeholder="example@domain.com" value="<?php echo $email; ?>">
            <?php echo "<p class='text-danger'>$errEmail</p>"; ?>
        </div>
    </div>
    <!--// BYE BYE SUBJECT!
    <div class="form-group">
        <label for="name" class="col-sm-2 control-label">Subject</label>

        <div class="col-sm-10">
            <input type="text" class="form-control" id="subject"
                   name="subject" placeholder="Message Subject" value="<?php # echo htmlspecialchars($_POST['subject']); ?>">
            <?php # echo "<p class='text-danger'>$errSubject</p>"; ?>
        </div>
    </div>
    -->
    <div class="form-group">
        <label for="message" class="col-sm-2 control-
label">Message</label>

        <div class="col-sm-10">
            <textarea class="form-control" rows="4" name="message">
                <?php echo $message; ?>
            </textarea>
            <?php echo "<p class='text-danger'>$errMessage</p>"; ?>
        </div>
    </div>
    <div class="form-group">
        <label for="human" class="col-sm-2 control-label">2 + 3 = ?
        </label>

        <div class="col-sm-10">
            <input type="text" class="form-control" id="human"
                   name="human" placeholder="Your Answer">
            <?php echo "<p class='text-danger'>$errHuman</p>"; ?>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">
            <input id="submit" name="submit" type="submit" value="Send"
                   class="btn btn-primary">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">
            <?php echo $result; ?>
        </div>
    </div>
</form>
</body>
</html>

1) Is there anyway not to jump up to the top of the screen when I submit the form? I believe this has something for making the action the page itself.

Because you're posting to the page and not providing a redirect, you'll always refresh the page, and you'll always reload the page at the top first. You could provide a Javascript function to scroll to a certain point, or redirect to an anchor tag on the page (though this would involve an unnecessary reload the way you're running it).

2) Is there any way to not need the 'subject' variable for the mail function? I'd love to not have the subject input on my form.

Sure! You can hardcode a subject into your PHP. Just replace your $subject = $_POST['subject']; with $subject = 'Message From Site'. Then remove the input from your form. You've already done this with your $from variable.

3) The biggest problem is that while the form runs, I have not received any emails from the form.

This is a more difficult issue. Emails from PHP scripts can be difficult to get through email provider spam filters. You may need to set some configuration or set specific headers in order to get emails sent to you.The mail() function accepts a set of headers as the fourth parameter, not just a "From" value - tweaking that may help. You can find TONS more info on debugging options aon the PHP docs, located here: http://php.net/manual/en/function.mail.php