登录&注册模态

时间:2016-12-11 18:16:47

标签: javascript php twitter-bootstrap semantic-ui

我在不同的php文件中创建了两个表单,一个是Login表单,另一个是Register表单,然后菜单中有两个Login和Register按钮 如何让这些按钮以模态显示其形式?

2 个答案:

答案 0 :(得分:1)

你可以使用ajax查询来完成它,所以我想给你一个如何做到这一点的例子:

HTML和JS:

<body>
  <div id="my_modal" class="modal"></div>
  <button  onclick="showLoginModal()">Login</button>
  <button  onclick="showRegisterModal()">Register</button>
</body>
<script>
      function showLoginModal(){   
          $.ajax({
            url:'php/login_content.php',
            type:"GET",
            cache:false,
            success:function(html){
              $("#my_modal").html(html);
              $('#my_modal').modal();
            }
          });
          event.preventDefault();
      }
      function showRegisterModal(){   
          $.ajax({
            url:'php/register_content.php',
            type:"GET",
            cache:false,
            success:function(html){
              $("#my_modal").html(html);
              $('#my_modal').modal();
            }
          });
          event.preventDefault();
      }
</script>

login_content.php文件:

<?php ?><div class="modal-content">
<form action="" method="post">
    User
    <input name="user_name" id="user_name" type="text"> Password
    <input name="user_pass" id="user_pass" type="password">
    <button type="submit" id="login">Login</button>
</form>

register_content.php文件:

<?php ?>
<div class="modal-content">
<form action="" method="post">
 Username
    <input name="user_name" id="user_name" type="text"> 
 Password
    <input name="user_pass" id="user_pass" type="password"> 
 Confirm Password
    <input name="user_pass" id="user_pass" type="password">
    <button type="submit" id="login"> Register</button>
</form></div>

问候!

答案 1 :(得分:0)

我会充实地发表我对达尔文的回答,以及你能看到我所说的话。

现在,我警告你有一个让用户进入登录页面的后备方式,以防万一javascript因为某种原因无法工作(并且有很多原因导致用户不会),但是那就是说。

此处的代码并不完全正确,因为它涉及多个文件,要获得完整的工作演示,请转到This Plunkr

&#13;
&#13;
// Code goes here

$(function() {
  $('.modal-btn').on('click', function(e) {
    e.preventDefault();
    var id = this.id;
    $('#login-register-modal').load(id + '_content.html', function(response, status) {
      if (status == 'success')
        $('#login-register-modal').modal();
    });


  })
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active">
          <a href="#">Link                 <span class="sr-only">(current)</span>
              </a>
        </li>
        <li>
          <a href="#">Link</a>
        </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown                 <span class="caret"></span>
              </a>
          <ul class="dropdown-menu">
            <li>
              <a href="#">Action</a>
            </li>
            <li>
              <a href="#">Another action</a>
            </li>
            <li>
              <a href="#">Something else here</a>
            </li>
            <li role="separator" class="divider"></li>
            <li>
              <a href="#">Separated link</a>
            </li>
            <li role="separator" class="divider"></li>
            <li>
              <a href="#">One more separated link</a>
            </li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-right">
        <div class="btn-group">
          <button id="login" class="btn btn-default modal-btn">Login</button>
          <button id="register" class="btn btn-default modal-btn">Register</button>
        </div>
      </form>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>
<div id="login-register-modal" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body…</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
&#13;
&#13;
&#13;

Register_content.html

<div class="modal-content">
  <div class="modal-header">
    <h2>Register</h2>
  </div>
  <div class="modal-body">

    <form class="form-horizontal" role="form">
      <div class="form-group">
        <label class="col-sm-2 control-label" for="inputEmail3">Email</label>
        <div class="col-sm-10">
          <input type="email" class="form-control" id="inputEmail3" placeholder="Email" />
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label" for="inputPassword3">Password</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" id="inputPassword1" placeholder="Password" />
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label" for="inputPassword3">Password</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" id="inputPassword2" placeholder="Password" />
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-default">Sign in</button>
        </div>
      </div>
    </form>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">
      Close
    </button>

  </div>
</div>

Login_content.html

<div class="modal-content">
  <div class="modal-header">
    Login
  </div>
  <div class="modal-body">

    <form class="form-horizontal" role="form">
      <div class="form-group">
        <label class="col-sm-2 control-label" for="inputEmail3">Email</label>
        <div class="col-sm-10">
          <input type="email" class="form-control" id="inputEmail3" placeholder="Email" />
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label" for="inputPassword3">Password</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" id="inputPassword3" placeholder="Password" />
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <div class="checkbox">
            <label>
              <input type="checkbox" /> Remember me
            </label>
          </div>
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-default">Sign in</button>
        </div>
      </div>
    </form>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">
      Close
    </button>

  </div>
</div>