这里有两个按钮,一个是登录按钮,第二个是去租.php .我的要求是,如果我单击登录按钮意味着我想在模态中显示登录表单,在该用户输入他们的凭据并成功登录后,这意味着我想重定向主页(索引.php(。现在我来到重点假设我点击去租.php意思是,首先我要检查这个用户是否已经登录,假设登录的意思是直接我必须重定向租金.php。假设他还没有登录意味着我必须在该用户输入电子邮件和密码后以模态显示登录表单,假设成功登录意味着现在我必须重定向租金.php ,怎么能做到这一点,请任何人更新我的答案。
function checkLogin(argument) {
$('#modalForm').modal('show');
}
// Login Check
$(document).ready(function(){
$("#loginbtn").click(function(e){
e.preventDefault();
var loginEmail = $('#loginEmail').val();
var password = $('#password').val();
$.ajax({
type:'POST',
url :"test_session.php",
data: {email: loginEmail, password: password},
success: function(response) {
if(response['status'] =='success'){
console.log(response);
window.location.href = "index.php";
}
else{
$("#update_Failed").show();
}
},
error:function(exception){
alert('Exeption:'+exception);
}
});
});
});
<?php
session_start();
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- Modal -->
<div class="modal fade" id="modalForm" role="dialog">
<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">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Login Form</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<p class="statusMsg"></p>
<form id="loginForm">
<div class="form-group" id="loginmobile">
<label for="email">Email address or Mobile:</label>
<input type="text" class="form-control stl" name="email" id="loginEmail" required="" data-msg-required="Please Enter Valid Email or Mobile no." aria-required="true">
</div>
<div class="form-group" id="loginmobile1" style="display:none">
<label for="email"> Mobile:</label>
<input type="number" class="form-control stl" name="email1" id="loginMobile" required="" data-msg-required="Please Enter Mobile no." aria-required="true">
</div>
<div class="form-group" id="otppwd">
<!-- <label for="pwd">Password Or OTP:</label> -->
<label for="pwd">Password :</label>
<input type="password" class="form-control stl" name="password" id="password" required="" data-msg-required="Please Enter Password" aria-required="true">
</div>
</br>
<div style="text-align: center;">
<button type="submit" id="loginbtn" class="btn btn-default contact_us_btn rentListLogin" >SUBMIT</button>
</div>
<div class="alert alert-danger" id="update_Failed" style="display:none;margin-top: 5%">
<br>
<strong>Username or Password is Incorrect</strong>
</div><br>
</form>
</div>
</div>
</div>
</div>
<a type="button" class="btn" onclick="checkLogin()">Login</a>
<?php
if(!empty($_SESSION['userName'])){
?>
<a href='rent.php' type="button" class="btn btn-default">Go to rent.php</a>
<?php
}
else
{
?>
<a type="button" class="btn btn-default" onclick="checkLogin()">go to rent.php</a>
<?php
}
?>
您可以选中设置变量以确定用户是否已登录。然后使用它来决定是显示登录模式还是转到租用.php页面。
<script>
<?php
if(!empty($_SESSION['userName'])){
var loggedIn = 1;
}
else {
var loggedIn = 0;
}
?>
function checkLogin(argument) {
if(loggedIn === 0) {
$('#modalForm').modal('show');
}
else {
window.location.href = "rent.php";
}
}
// Login Check
</script>