在bootstrap模式中提交表单

时间:2016-08-13 22:49:23

标签: javascript php jquery twitter-bootstrap

我有一个使用modal注册新用户的页面这是模态代码:

<div id="register" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">New User</h4>
      </div>
      <div class="modal-body">

      <form id="new_customer" action="../model/php/action.php" method="post">
     <div class="form-group">
  <label for="usr">username:</label>
  <input type="text" name="user" class="form-control" id="usr">
</div>
 <div class="form-group">
  <label for="em">email :</label>
  <input type="text" name="email" class="form-control" id="em">
</div>
<div class="form-group">
  <label for="pwd">password :</label>
  <input type="password" name="pwd" class="form-control" id="pwd">
</div>
        <button type="submit"  id="signup" name="signup" class="btn btn-success btn-lg">signup<span class="glyphicon glyphicon-user"></span></button>
     </form>   </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-info" data-dismiss="modal">close</button>
      </div>
    </div>

  </div>
</div>

这里是示例操作页面:

<?php
   if(isset($_POST['signup']))
   {
       if(empty($_POST['user']) || empty($_POST['email']) || empty($_POST['pwd']))
       {
       echo $lang['empty'];
       }
   }
   ?>

我希望在同一个对话框中查看结果。

1 个答案:

答案 0 :(得分:3)

这个ajax请求你的问题,如果你不理解它,请阅读有关jquery和ajax请求。

http://api.jquery.com/jquery.ajax/
https://learn.jquery.com/

ajax请求它不会调用页面的重新加载,因此你的模态仍会显示:

$.ajax({
    type: "POST",
    data: {user: $('#usr').val(), email: $('#em').val(), pwd: $('#pwd').val()},
    url: "/model/php/action.php",
    success: function(data){
          //here data will contain all output from .php, so for your script it will equal to $lang['empty'], you ca do whatever you want with data
          //f.e. set to modal
    },
    error: function(data) {
        console.log(data); //error message
    }
});