通过Php验证后弹出Bootstrap Modal

时间:2015-06-06 07:05:26

标签: php twitter-bootstrap validation twitter-bootstrap-3

如果发生某些验证错误,我想弹出Bootstrap模式并将其显示在模态上。页面重新加载后会发生Php验证,因此如果发生错误,bootstrap模式会弹出相应的错误。

这是模态的代码。

    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content" >
        <div class="modal-header" style="padding:35px 50px;">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h3><span class="glyphicon glyphicon-lock"></span> Login</h3>
        </div>
        <div class="modal-body" style="padding:40px 50px;">
          <form role="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

            <div class="form-group">
              <label for="usrname"><span class="glyphicon glyphicon-user"></span> Username</label>
              <input type="text" class="form-control" id="u_email" name="u_email" value="<?php echo $u_email;?>" placeholder="Enter email">
              <span class="error"> <?php echo $u_emailErr;?></span>
            </div>
            <div class="form-group">
              <label for="psw"><span class="glyphicon glyphicon-eye-open"></span> Password</label>
              <input type="text" class="form-control" id="password" name="password" value="<?php echo $password;?>" placeholder="Enter password">
              <span class="error"> <?php echo $passwordErr; echo $count;?></span>
            </div>
            <div class="checkbox">
              <label><input type="checkbox" value="" checked>Remember me</label>
            </div>
              <button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-off"></span> Login</button>
         </form>
        </div>
        <div class="modal-footer">
          <button type="submit" class="btn btn-danger btn-default pull-left" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
          <p>Not a member? <a href="#" data-dismiss="modal" data-toggle="modal" data-target="#myModal1" data-backdrop="static" >Sign Up</a></p>
          <p>Forgot <a href="#">Password?</a></p>
        </div>
      </div>



    </div>
  </div>

这是php验证。

<?php
// define variables and set to empty values
$u_emailErr = $passwordErr = "";
$u_email = $password = "";
$count = 0;

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{  
   if (empty($_POST["u_email"]))
    {
     $u_emailErr = "Email is required";
     $count++;
    } 
   else 
   {
     $u_emailErr = test_input($_POST["u_email"]);
     // check if e-mail address is well-formed
     if (!filter_var($u_email, FILTER_VALIDATE_EMAIL)) 
     {
       $u_emailErr = "Invalid email format"; 
       $count++;
     }
   }

   if (empty($_POST["password"])) 
   {
     $passwordErr = "Password is required";
     $count++;
   } 
   else 
   {
     $passwordErr = test_input($_POST["u_email"]);

   } 
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>  

任何人都可以帮助...........?

1 个答案:

答案 0 :(得分:0)

您必须在无效条件下添加以下代码

else {
    echo "<script type='text/javascript'>
    $(document).ready(function(){
    $('.modal-dialog').modal('show');
    });
    </script>";
    }