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

报告相同问题?

悬赏问题

  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错