在数据库中插入后关闭Modal引导程序窗口

时间:2016-10-27 08:53:59

标签: php jquery ajax twitter-bootstrap

我有模态引导窗口,使用AJAX和JQUERY在数据库中插入数据,但插入后我无法关闭窗口,我还要显示成功插入的消息,请参阅下面的代码

----模态窗口----

    <!-- Modal  employer-->
<div class="modal fade" id="employer" tabindex="-1" role="dialog" 
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" 
                   data-dismiss="modal">
                       <span aria-hidden="true">&times;</span>
                       <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">

                </h4>
            </div>

            <!-- Modal Body -->
            <div class="modal-body">

                <form role="form" action='insert.php' method='post' id='myform'>
                  <div class="form-group">
                    <label for="exampleInputEmail1">Nom</label>
                      <input type="text"  name="nom"class="form-control"
                      id="exampleInputEmail1" placeholder="Nom"/>
                  </div>
                  <div class="form-group">
                    <label for="exampleInputPassword1">Prenom</label>
                      <input type="text" name="prenom" class="form-control"
                          id="exampleInputPassword1" placeholder="Prenom"/>
                  </div>
                                    <div class="form-group">
                    <label for="exampleInputPassword1">telephone</label>
                      <input type="text" name="telephone"class="form-control"
                          id="exampleInputPassword1" placeholder="telephone"/>
                  </div>
      <div class="form-group">
                    <label for="exampleInputPassword1">Fonction</label>
                      <input type="text" name="fonction" class="form-control"
                          id="exampleInputPassword1" placeholder="Fonction"/>
                  </div>
                      <div class="form-group">
                    <label for="exampleInputPassword1">Salire</label>
                      <input type="text" name="salaire" class="form-control"
                          id="exampleInputPassword1" placeholder="Salire"/>
                  </div>




            </div>

            <!-- Modal Footer -->
            <div class="modal-footer">

                <button type="button" id='insert' class="btn btn-primary">
                    Save changes
                </button>
                  </form>
            </div>
        </div>
    </div>
</div>

---- insert.php ----

<?php
require('config.php');
 $nom = $_POST['nom'];
 $prenom = $_POST['prenom'];
  $telephone = $_POST['telephone'];
   $fonction = $_POST['fonction'];
    $salaire = $_POST['salaire'];
 $sql = "insert into employer (nom, prenom,telephone,fonction,salaire) values ('$nom','$prenom','$telephone','$fonction','$salaire')";

 if(mysqli_query($con, $sql)){
 echo 'success';
?>



 <script type="text/javascript">
$(document).ready(function(){

        $('#employer').modal('hide');

});
</script>
 <?php

  }
?>

---- insert.js ----

 $('#myform').submit(function(){
    return false;
});

$('#insert').click(function(){
    $.post(     
        $('#myform').attr('action'),
        $('#myform :input').serializeArray(),
        function(result){
            $('#result').html(result);
        }
    );
});

2 个答案:

答案 0 :(得分:0)

试试这个:

$('#modal').modal('close');

它会关闭模态。

答案 1 :(得分:0)

当您从ajax请求获得响应时,请关闭模态窗口。

    $('#employer').modal('hide');

根据你的例子:

$('#insert').click(function(){
    $.post(     
        $('#myform').attr('action'),
        $('#myform :input').serializeArray(),
        function(result){
            $('#employer').modal('hide'); // ADD CODE FOR CLOSE MODAL
            $('#result').html(result);
        }
    );

});