我正在尝试通过 php 创建登录,它在静态页面中工作,但不是以引导模式形式..
如果我正在输入任何数据,它会显示成功并发出警报,而不是验证我的数据!
我还在登录中实现了验证系统,这意味着只有在电子邮件验证后才能登录,但它也不适用于 ajax。
请帮忙!如何为此php登录代码编写ajax?
我尝试过的只是在代码中给出。
登录.php
<?php
$msg = "";
if (isset($_POST['login_button'])) {
$con = new mysqli('localhost', 'root', '', 'research');
$email = $con->real_escape_string($_POST['email']);
$password = $con->real_escape_string($_POST['password']);
if ($email == "" || $password == "")
$msg = "Please check your inputs!";
else {
$sql = $con->query("SELECT id, password, isEmailConfirmed FROM users WHERE email='$email'");
if ($sql->num_rows > 0) {
$data = $sql->fetch_array();
if (password_verify($password, $data['password'])) {
if ($data['isEmailConfirmed'] == 0)
$msg = "Please verify your email!";
else {
$msg = "You have been logged in";
}
} else
$msg = "Please check your inputs!";
} else {
$msg = "Please check your inputs!";
}
}
}
?>
index.php 中包含的脚本
<script>
$(document).ready(function(){
$('#login_button').click(function(){
var email = $('#email').val();
var password1 = $('#password1').val();
if(email!= '' && password1!= '')
{
$.ajax({
url:"login.php",
type:"POST",
data: {email:email, password1:password1},
success:function(data)
{
//alert(data);
if(data == 'Please check your inputs!')
{
alert("Wrong Data");
}
else
{
$('#elegantModalForm').hide();
location.reload();
alert("Successful");
}
}
});
}
else
{
alert("Both Fields are required");
}
});
/* $('#logout').click(function(){
var action = "logout";
$.ajax({
url:"action.php",
method:"POST",
data:{action:action},
success:function()
{
location.reload();
}
});
});*/
});
</script>
用 index.php 编写的模态表单引导代码
<!-- Modal -->
<div class="modal fade" id="elegantModalForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<!--Content-->
<div class="modal-content form-elegant">
<!--Header-->
<div class="modal-header text-center">
<h3 class="modal-title w-100 dark-grey-text font-weight-bold my-3" id="myModalLabel"><strong>Sign in</strong></h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<!--Body-->
<div class="modal-body mx-4">
<!--Body-->
<div class="md-form mb-5">
<input type="email" name="email" id="email" class="form-control validate">
<label data-error="wrong" data-success="right" for="Form-email1">Your email</label>
</div>
<div class="md-form pb-1">
<input type="password" id="password1" name="password1" class="form-control validate">
<label data-error="wrong" data-success="right" for="Form-pass1">Your password</label>
<p class="font-small blue-text d-flex justify-content-end">Forgot <a href="#" class="blue-text ml-1">
Password?</a></p>
</div>
<div class="text-center mb-3">
<button type="button" class="btn btn-primary display-4" name="login_button" id="login_button">Sign in</button>
</div>
<p class="font-small dark-grey-text text-right d-flex justify-content-center mb-3 pt-2"> or Sign in
with:</p>
<div class="row my-3 d-flex justify-content-center">
<!--Facebook-->
<button type="button" class="btn btn btn-white btn-rounded mr-md-3 z-depth-1a"><i class="fab fa-facebook-f text-center"></i></button>
<!--Twitter-->
<button type="button" class="btn btn-white btn-rounded mr-md-3 z-depth-1a"><i class="fab fa-twitter"></i></button>
<!--Google +-->
<button type="button" class="btn btn-white btn-rounded z-depth-1a"><i class="fab fa-google-plus-g"></i></button>
</div>
</div>
<!--Footer-->
<div class="modal-footer mx-5 pt-3 mb-1">
<p class="font-small grey-text d-flex justify-content-end">Not a member? <a href="#" class="blue-text ml-1">
Sign Up</a></p>
</div>
</div>
</div>
</div>
<!-- Modal -->