Javascript AJAX响应不会在第二次单击按钮时显示
我有一个简单的登录表单,其中通过AJAX调用传递了值。问题是当我第一次输入错误的电子邮件或密码时,它会向我显示错误消息。第二次,如果输入错误,则不会显示错误。我哪里做错了请提供建议/帮助 表格Javascript AJAX响应不会在第二次单击按钮时显示,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个简单的登录表单,其中通过AJAX调用传递了值。问题是当我第一次输入错误的电子邮件或密码时,它会向我显示错误消息。第二次,如果输入错误,则不会显示错误。我哪里做错了请提供建议/帮助 表格 <?php if (isset($_SESSION['login_email']) && !empty($_SESSION['login_email'])) { //header('Location:profile.php'); ?> <script> lo
<?php
if (isset($_SESSION['login_email']) && !empty($_SESSION['login_email'])) {
//header('Location:profile.php');
?>
<script> location.replace("profile.php"); </script>
<?php
} else {
?>
<div class="login_form">
<h1 class="login_heading">Login</h1>
<div class="alert-error"></div>
<div class="alert-success"></div>
<div class="login">
<form method="post" action="">
<label >Email</label>
<input class="inputs_login" type="email" name="email" id="email" placeholder="email" >
<label>Password</label>
<input class="inputs_login" type="password" name="password" id="password" placeholder="password"><br>
<input type="button" name="login_submit" id="login_submit" value="login">
</form>
</div>
</div>
<?php
}
?>
location.replace(“profile.php”);
登录
电子邮件
密码
Ajax
<script>
$(document).ready(function() {
$('#login_submit').click(function(e){
//e.preventDefault();
var email = $("#email").val(),
password = $("#password").val();
var proceed = true;
if(proceed){
post_data= { 'Email': email, 'Password': password};
$.post('login_index.php', post_data, function(response){
//load json data from server and output message
if(response.type == 'error')
{
output=$('.alert-error').html(response.text);
}else{
location.href="profile.php";
}
$(".alert-error").delay(3200).fadeOut(300);
}, 'json');
}
});
});
</script>
$(文档).ready(函数(){
$(“#登录_提交”)。单击(函数(e){
//e、 预防默认值();
var email=$(“#email”).val(),
密码=$(“#密码”).val();
var=true;
如果(继续){
post_数据={'Email':Email,'Password':Password};
$.post('login\u index.php',post\u数据,函数(响应){
//从服务器加载json数据并输出消息
如果(response.type==“error”)
{
输出=$('.alert error').html(response.text);
}否则{
location.href=“profile.php”;
}
$(“.alert error”)。延迟(3200)。淡出(300);
}“json”);
}
});
});
php
<?php
include "db/db.php";
session_start();
if ($_POST) {
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(array(
'type' => 'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
if (isset($_POST['Email']) && isset($_POST['Password'])) {
$email = filter_var($_POST["Email"], FILTER_SANITIZE_STRING);
$pwd = filter_var($_POST["Password"], FILTER_SANITIZE_STRING);
$query = mysqli_query($con, "select * from customers where email='$email' and password='$pwd'");
$count = mysqli_num_rows($query);
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
if ($row) {
$_SESSION['login_email'] = $row['email'];
$output = json_encode(array(
'type' => 'message',
'text' => 'Hi ' . $email . ' You are successfully login'
));
die($output);
} else {
$output = json_encode(array(
'type' => 'error',
'text' => 'Could not Login! Please check your email/password OR <a href="index.php?page=register_account">REGISTER FREE ACCOUNT</a> .'
));
die($output);
}
}
}
?>
尝试给出$(“.alert error”).html(“”)$(“.alert error”).show()在//e.preventDefault()之后@Anopll yes it Worked您了解这个问题吗?请尝试给出$(“.alert error”).html(“”)$(“.alert error”).show()在//e.preventDefault()之后@是的,成功了你明白这个问题吗?