doupingmao1903 2015-07-12 04:19
浏览 21
已采纳

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> 
  • 写回答

2条回答 默认 最新

  • drvjlec1767 2015-07-12 05:07
    关注

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题