php - 防止引导模式在表单提交后关闭

标签 php forms twitter-bootstrap-3 modal-dialog

我在网上搜索并找到了一些解决方案,但它们都使用 JS 或 AJAX,并且只在一定程度上提供了帮助。我是 PHP 的新手,对 AJAX 一无所知,所以如果这里有人可以为我提供使用 PHP 和 HTML 或至多 JS 的解决方案。

我在客户网站页 footer 分的 bootstrap 3 模式中有一个非常简单的订阅表单。它的 PHP 验证订阅者使用他们的官方或公司电子邮件地址仅用于订阅和一些其他常见/更简单的验证。

该表单运行良好,但问题是一旦用户单击提交模式关闭,并且用户无法看到成功或失败消息,直到他们从触发按钮重新打开模式。我希望模式即使在用户提交表单后也保持打开状态并显示表单提交是否成功。我希望我能够正确解释我的问题。这是我的 HTML 和 PHP 供您引用:

HTML:

  <div id="footer">   
    <div class="container">
      <div class="col-md-6">
        <div id="SubscribeModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">✕</button>
              </div>
              <div class="modal-body">
                <?php include('subscribe.php') ?>
              </div>
              <div class="modal-footer"></div>
            </div><!-- /.modal-content -->
          </div><!-- /.modal-dalog -->
        </div><!-- /.modal -->
        <a data-toggle="modal" href="#SubscribeModal" class="text-muted">Subscribe</a>
      </div>
    </div>
  </div>

PHP:

<?php
    if(isset($_POST['subscribe'])){ 
    /* Configuration */
    $subject = 'Please subscribe me to your Risk Alerts. Thank you.'; // Set email subject    line here
    $mailto  = 'xyz@company.com'; // Email address to send form submission to
    /* END Configuration */
    if(empty($_POST['firstname'])){
        $error = "Please add your first name";
    }elseif(empty($_POST['lastname'])){
        $error = "Please add your last name";
    }elseif(empty($_POST['email'])){
        $error = "Please add your business email";    
    }else{ 
        $firstname      = $_POST['firstname'];
        $lastname       = $_POST['lastname'];
        $email          = $_POST['email'];

        // HTML for email to send submission details
        $body = "
        <br>
        <p>The following information was submitted through the contact form on your website:</p>
        <p><b>Name</b>: $firstname $lastname<br>
        <b>Email</b>: $email<br>
        ";


        $headers = "From: $firstname $lastname <$email> \r\n";
        $headers .= "Reply-To: $email \r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $message = "<html><body>$body</body></html>";

        //build list of not allowed providers as lowercase
        $NotAllowedClients = array("aol","applemail","comcast","entourage","gmail","hotmail","outlook");

        preg_match_all('/\@(.*?)\./',$email,$clientarr);            
        $client = strtolower($clientarr[1][0]);            

        if(in_array($client,$NotAllowedClients)){
            //Failed
            $notice = "<div class=\"row-fluid\">
            <div class=\"span12\">
                <h3>Subscription Failed!</h3>
                <p>Please use an official/company email address to subscribe.  <a href=\"?\">Try again</a></p>
            </div>
        </div>"; 
        }else{
            //Passed
            //echo $message;
            mail($mailto, $subject, $message, $headers);
            $notice = "<div class=\"row-fluid\">
            <div class=\"span12\">
                <h3>Subscription successful!</h3>
                <p>Thank you for taking the time to subscribe to our weekly Risk Alerts.</p>
            </div>
        </div>";
        }
    }
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Risk Alerts</title>
</head>
<body>
<?php
if(isset($notice)){
echo $notice;
}else{
    //Show error for missing field
    if(isset($error)){echo $error;}
?>
    <div class="thumbnail center well well-small text-center">
        <form id="subscription" method="post" action="" class="validate" novalidate>
            <div class="row">
                <div class="col-xs-6 col-md-6">
                    <input type="text" name="firstname" value="" class="form-control input-md" placeholder="your first name"  />
                </div>
                    <div class="col-xs-6 col-md-6">
                <input type="text" name="lastname" value="" class="form-control input-md" placeholder="your last name"  />
                </div>
            </div><p></p>
            <input type="text" value="" name="email" class="form-control" placeholder="your email address" required /><br />
            <div class="clear">
                <input type="submit" value="Subscribe" name="subscribe" class="btn btn-md btn-primary button" />
            </div>
        </form>
    </div> 

    <?php
    }
    ?>
</body>
</html>

最佳答案

我没有检查您的实际 PHP,但假设它有效,我会使用 ajax 提交表单并处理响应。

因此,请尝试以下操作: 为模态内容添加一个 ID:

          <div class="modal-body" id="modal_content">
            <?php include('subscribe.php') ?>
          </div>

然后将您的提交按钮更改为:

<a href="javascript:void(0)" class="text-muted" id ="submit_button">Subscribe</a>

然后将此 jquery 添加到表单页面的底部(用正确的 url 替换 DIRECT_URL_TO_SUBSCRIBE):

jQuery(function ($){
    $('#submit_button').click(function(){
        var post_url = 'DIRECT_URL_TO_SUBSCRIBE.php';

        $.ajax({
            type : 'POST',
            url : post_url,
            data: $('#subscription').serialize(), //ID of your form
            dataType : 'html',
            async: true,
            beforeSend:function(){
                //add a loading gif so the broswer can see that something is happening
                $('#modal_content').html('<div class="loading"><img scr="loading.gif"></div>');
            },
            success : function(data){
                $('#modal_content').html(data);
            },
            error : function() {
                $('#modal_content').html('<p class="error">Error in submit</p>');
            }
        });
    })           
});

关于php - 防止引导模式在表单提交后关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22685283/

相关文章:

javascript - 错误不会显示在以 html 形式提交的表单上的 span 标记中 - Jquery

php - Codeigniter 验证复选框组

jquery - ajax 调用后重置 CSS 徽章内容

php - 在 Windows 和 Linux 机器上通过 Web 表单更新 SVN

php - 使用外键创建表时 Laravel 迁移错误

php - 如何在 Symfony 3 中设置默认表单所需状态(并在构建器中覆盖)?

php - 获取laravel中带有特定标签的所有数据

PHP JSON 解析,只显示三个参数之一

javascript - Bootstrap Summernote 重置/清除

jquery-ui - 编辑 Bootstrap 模板,动态添加 jarvisWidget 并在页面加载后重新生成小部件