【问题标题】:Email not sending in form Bootstrap电子邮件未以 Bootstrap 形式发送
【发布时间】:2014-05-27 12:36:26
【问题描述】:

我在 Bootstrap 3 中有一个位于模态框内的表单。当我单击“提交”按钮时,它会重定向到 process.php,它应该向我的电子邮件地址发送一封电子邮件。没有。

有人知道为什么会这样吗?表格如下:

<div class="modal fade" id="contactPop" tabindex="-1" role="dialog" aria-labelledby="contactModal" aria-hidden="true">
     <div class="modal-dialog">
        <div class="modal-content">
           <div class="modal-header">
              <!-- Close button. -->
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <!-- Title. -->
              <h3 class="modal-title" style="font-family: 'Lato', sans-serif; font-weight: 700;">
                 Contact Us <small style="font-family: 'Lato', sans-serif; font-weight:400;"> You matter to us.</small>
              </h3>
           </div>

           <!-- User input fields. -->
           <form class="contact" action="process.php" method="post">
           <div class="modal-body">
              <div class="row">
                 <div class="col-sm-6">
                    <label class="control-label">Subject</label>
                    <input type="text" class="form-control" required="required" placeholder="I aciddentally the website!" name="subject">
                 </div>
                 <div class="col-sm-6">
                    <label class="control-label">Username</label>
                    <input type="text" class="form-control" required="required" placeholder="Notch" name="username">
                 </div>
              </div>
              <br>

              <div class="row">
                 <div class="col-sm-6">
                    <label class="control-label">Message</label>
                    <textarea class="form-control" rows="8" style="resize: vertical;" required="required" placeholder="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget fermentum justo, sit amet semper." name="message"></textarea>
                 </div>

                 <div class="col-sm-6">
                    <label class="control-label">Email</label>
                    <input type="email" class="form-control" required="required" placeholder="johnsmith@example.com" name="email">
                 </div>

                 <div class="col-sm-6" style="line-height: 21px;">
                    <br>
                    <p>Responses are usually received within 1-2 business days.</p>
                    <p>Please be as clear and concise as possible, in order to help us process your inquiry faster.</p>
                 </div>
              </div>
           </div>

           <!-- Close & submit buttons. -->
           <div class="modal-footer">
              <button type="button" class="btn btn-info" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
              <button type="button" value=" Send" class="btn btn-success" type="submit" id="submit"><i class="glyphicon glyphicon-inbox"></i> Submit</button>
           </div>
        </div>
     </div>
  </div>

还有发送表单的process.php,在这里:

<?php
//add the recipient's address here
$myemail = 'stanleyreale@gmail.com';

//grab named inputs from html then post to #thanks
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
echo "<span class=\"alert alert-success\" >Your message has been received, and we will get                 back to you as soon as possible. Here is what you submitted:</span><br><br>";
echo "<stong>Name:</strong> ".$name."<br>";   
echo "<stong>Email:</strong> ".$email."<br>"; 
echo "<stong>Message:</strong> ".$message."<br>";

//generate email and send!
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}
?>

然后在索引中还有应该提交表单的 javascript。

 <script type="text/javascript">
     $(document).ready(function () {
$("input#submit").click(function(){
    $.ajax({
        type: "POST",
        url: "process.php", //process to mail
        data: $('form.contact').serialize(),
        success: function(msg){
            $("#thanks").html(msg) //hide button and show thank you
            $("#form-content").modal('hide'); //hide popup  
        },
        error: function(){
            alert("failure");
        }
    });
});
});
  </script>

如果有人可以提供帮助,将不胜感激!

【问题讨论】:

  • 那么表单是否发布了任何数据?
  • 如果你说的是当它进入 process.php 时,不,它只是一个空白页。抱歉回复晚了 - 我没有看到你的评论。

标签: php html forms twitter-bootstrap


【解决方案1】:

尝试返回false;

$("input#submit").click(function(){
    $.ajax({
        type: "POST",
        url: "process.php", //process to mail
        data: $('form.contact').serialize(),
        success: function(msg){
            $("#thanks").html(msg) //hide button and show thank you
            $("#form-content").modal('hide'); //hide popup  
        },
        error: function(){
            alert("failure");
        });

return false;

    });

另外,因为您使用 jQuery Ajax 来提交数据。您不需要 HTML 中的表单标签。您被重定向是因为您的表单标签中提到了 action='process.php' 并且没有 return false; 在您的 jQuery 中用于 onClick 事件强>

Email not sending

确保你在 jquery ajax 中指定了正确的路径

url: "process.php", //process to mail

【讨论】:

  • 我删除了
    标签,但是提交按钮停止工作,所以我又把它们放回去了。我添加了 return false; ,但我仍然没有收到电子邮件。
  • @user3027207 你能发布你的项目文件夹结构吗?只是看看你在哪里 process.php 文件
  • 它看起来像这样:pastebin.com/WvN8En7ZIndex.html 和 process.php 在 www。顺便说一句,www、public_html 和 public_ftp 都是文件夹。
  • @user3027207 我还注意到您的 ajax 缺少缓存:false,dataType:'JSON'
  • @user3027207 看看 console.log($('form.contact').serialize());检查数据是否正确序列化
【解决方案2】:

我知道这是很久以前提出的问题。但答案是

<button **type="button"** value=" Send" class="btn btn-success" **type="submit"** id="submit"><i class="glyphicon glyphicon-inbox"></i> Submit</button>

您不需要 type="submit"type="button"。删除 type="submit" 会阻止你重定向。

或者您可以使用 type="submit" 并在您的索引 javascript 中使用

$("input#submit").click(function(e){
    e.preventDefault();
    $.ajax({
        type: "POST",
         ....
         ....

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多