我的问题的简短版本: 我点击“登录”弹出一个登录表单。然后用户可以通过输入电子邮件和密码登录。提交表单并成功后,用户将重定向到他的个人资料。
我想要的: 如果验证中出现错误,例如无效的电子邮件或密码错误,则不应提交登录表单,而是在登录弹出窗口中显示错误消息。因此,如果出错,弹出窗口应保持打开状态。
代码:
弹出登录表单:
<div id="added_login">
<div class="form_title">
<h3>Anmelden</h3>
<img id="form_close" src="images/close.png"/>
</div>
<form id="login_form" action="includes/login.inc.php" method="POST">
<img src="images/or_line.png" />
<p>Melde dich mit deiner E-Mail Adresse an</p>
<input id="email" type="text" name="email" placeholder="E-Mail Adresse oder Benutzername">
<input id="password" type="password" name="password" placeholder="Passwort eingeben">
<div class="error_msg_container"></div>
<input id="submit" class="button button_highlight" type="submit" name="submit_login" value="Anmelden">
<div class="login_footer">
<p class="forget_password">Passwort vergessen?</p>
<p id="loginform_register" class="button_register">Registrieren</p>
</div>
</form>
</div>
弹出窗体的jquery / ajax部分
$("#login_form").submit(function(e){
e.preventDefault();
var email = $("#email").val();
var password = $("#password").val();
var submit = $("#submit").val();
$.ajax({
type: "POST",
url: "includes/login.inc.php",
data: {
email: email,
password: password,
submit_login: submit
},
success: function(data){
$("#error_msg_container").html(data);
},
error: function(){
alert("ERROR");
}
})
});
数据库的php部分:
<?php
session_start();
//this function checks the user input
function IsEmail($email){
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
//if user input is an email then return true
return true;
}else{
//if user input isnt an email then return false
return false;
}
}
if (isset($_POST['submit_login'])) {
include_once 'db_connect.inc.php';
$loginid = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
if(empty($loginid)){
//Check for empty email
echo "<p>E-Mail cannot be empty.</p>";
}elseif(empty($password)){
//Check for empty password
echo "<p>Password cannot be empty.</p>";
}else{
$check_email = IsEmail($loginid);
if($check_email){
//is input an email
$sql = "SELECT * FROM users WHERE email='$loginid'";
$result = mysqli_query($conn, $sql);
}else{
//is email a username
$sql = "SELECT * FROM users WHERE username='$loginid'";
$result = mysqli_query($conn, $sql);
}
$resultCheck = mysqli_num_rows($result);
//Check for user existing
if($resultCheck < 1){
echo "<p>No such a user found.</p>";
}else{
if($row = mysqli_fetch_assoc($result)){
//Dehashing password
$hashedPasswordCheck = password_verify($password, $row['password']);
//Check for correct password
if($hashedPasswordCheck == false){
header("Location: ../index.php?login=wrong_password");
exit();
}elseif($hashedPasswordCheck == true){
if($row[active] == 0){
echo "<p>Your accent have to set on active</p>";
}else{
//Log in the user
$_SESSION[user_id] = $row[id];
$_SESSION[user_firstname] = $row[firstname];
$_SESSION[user_lastname] = $row[lastname];
$_SESSION[user_gender] = $row[gender];
$_SESSION[user_username] = $row[username];
$_SESSION[user_email] = $row[email];
$_SESSION[user_level] = $row[level];
$_SESSION[user_points] = $row[points];
$_SESSION[loggedIn] = true;
header("Location: ../index.php?login=success");
exit();
}
}
}
}
}
}else{
header("Location: ../index.php");
exit();
}
现在的问题是什么都没发生..弹出登录窗口保持打开但错误信息没有显示,即使输入正确也没有发生任何事情..
希望你们能帮助我。 THX
答案 0 :(得分:1)
There is issue in your jquery code.
在ajax成功函数中,你提到了div id&#39; error_msg_container&#39;这实际上是html中的一个类。 代码应该是
success: function(data){
$(".error_msg_container").html(data);
}