简体   繁体   English

提交表格时防止模式关闭

[英]Prevent modal from closing when submiting form

I have a form inside a modal, I want to prevent the modal from closing and refreshing the page after submiting the form if there's errors on some fields and show error messages inside the modal, if everything is okay the modal shoud submit the form and refresh the page. 我在模态中有一个表单,如果某些字段上有错误,我想防止模态在提交表单后关闭并刷新页面,并在模态中显示错误消息,如果一切正常,模态应该提交表单并刷新这一页。

This is my code : 这是我的代码:

<div class="modal fade" id="addTeam">
<div class="modal-dialog rap-modal-thumb">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">X</span></button>
        <h4 class="modal-title" id="myModalLabel">Add team</h4>
        </div>
        <div class="modal-body"><br>
              <div class="page-wrap">
                <form action= "<?php echo $action; ?>" enctype="multipart/form-data" method="post">
                  <div class="container">
                        <div class="row">                                                   
                          <div class="input-group input-group-icon">
                              <div class="col-third input-group-icon">
                                <input type="text" id="name" name="name" placeholder="Nom*" required="required">
                                <div class="input-icon"><i class="fa fa-users"></i></div>
                              </div>
                              <div class="col-third input-group-icon">
                                <input type="text" id="abreviation" name="abreviation" placeholder="Abréviation*" required="required">
                                <div class="input-icon"><i class="fa fa-flag-o"></i></div>
                                </div>
                        <br>
                        * Required fields
                        <br><br>
                        <?php
                            if (isset($error)) 
                                echo '<div style="color:#fff;" class="alert bg-danger" role="alert"><em class="fa fa-lg fa-warning">&nbsp;&nbsp;</em>',$erreur,'</div>';  
                        ?>
                        <button type="submit" id="addTeam" name="addTeam" class="btn btn-primary">Ajouter</button>
                    </div>
                </form>
            </div>
            <br>
        </div>
    </div>
</div>

In php if for example the name of the team is less than 6 characters then show the error message inside the modal without closing it. 例如,在php中,如果团队的名称少于6个字符,则在模式内显示错误消息而不关闭它。 In my case here the message appear after the page is refreshed and the show button of the modal is clicked again. 就我而言,在刷新页面并再次单击模式的显示按钮之后,将显示该消息。

if (isset($_POST['addTeam'])) {
    $name= $_POST['name'];
    if (strlen($name)<3){
        $error = 'Name must be atleast 6 characters';
    }
    else{
        $action = "uploadTeam.php";
    }
}

Anyway to do that ? 无论如何要这样做?

If your form is submited by default by the browser, it'll auto-refresh the page (to create a new HTTP POST request to the server) 如果您的表单是默认情况下由浏览器提交的,它将自动刷新页面(以向服务器创建新的HTTP POST请求)

You have to use AJAX to call the server on background and avoid reloading the content: 您必须使用AJAX在后台调用服务器,并避免重新加载内容:

// Create an event listener to catch "when the form is submited"
$('.modal-body form').submit(function(event) {
    // to stop the form from submitting (and the page reloading, so the modal will not close)
    event.preventDefault(); 

    // Call server manually and send form data (see Mikey's comment)
    $.post('my.url.com/file.php', $(this).serialize(), function(err, result) {
      if (err) { 
        // Display error on form 
      } else {
        // Success... Redirect ?
        window.location = 'my.new.url/path.html'
      }
    })
});

You will need to override some client behavior for this. 为此,您将需要覆盖一些客户端行为。 In fact, it will be easier for you to do your validation all on the client side (if possible). 实际上,您将更容易在客户端进行所有验证(如果可能的话)。 Override the form's default behavior, validate, and then return true or false based on the results of your validation function: 覆盖表单的默认行为,进行验证,然后根据验证函数的结果返回truefalse

<script>
    $('form').on('submit', function(e) {
        if (validate()) { //your validation function here
            return true;
        }
        return false;
    });
    function validate() {
        if (!$('input[name=name]').val() || $('input[name=name]').val().length < 3) {
            return false;
        }
        return true;
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM