gpt4 book ai didi

javascript - 使用 Ajax 登录 PHP

转载 作者:行者123 更新时间:2023-12-03 09:46:44 24 4
gpt4 key购买 nike

我正在尝试使用ajax进行登录,我希望用户登录,如果用户名(电子邮件)和密码正确,则页面应该使用方法.load来加载页面“perfil.php”,我不会发布代码,因为它不相关。但是,即使我输入随机用户/密码,也不会发生任何事情。

我刚刚开始使用 SESSION 和 AJAX,因此我们将不胜感激,顺便说一句,我现在不需要谈论哈希密码的答案。

索引

<?php
session_start();
if(!empty($_SESSION['login_user'])){
header('Location: user/perfil.php');
}
?>

<form class="form-signin" id="form-signin" method="post" action="">
<h2 class="form-signin-heading">Sign in</h2>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required="true" autofocus="true">
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required="true">
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Lembrar-me
</label>
</div>
<button id="entrar" class="btn btn-lg btn-primary btn-block" type="submit" name="submit">Sign in</button>

login.php

<?php
include_once '../config/db.php';
session_start();
if (isSet($_POST['email']) && isSet($_POST['password'])) {
try {
$pdo = Database::connect();
$email = $_POST['email'];
$password = $_POST['password'];

$query= "SELECT id FROM utilizador WHERE email = '$email' and pass = '$password'";

$stmt = $pdo->prepare($query);
$stmt->execute();
$num=$stmt->rowCount();

$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($num > 0)
$_SESSION['login_user']=$row['id'];
echo $row['id'];
}
Database::disconnect();
}
catch (Exception $e){
echo 'Exception: ', $e->getMessage(), "\n";
}
}
?>

javascript

$('#entrar').click(function()
{
var email = $("#inputEmail").val();
var password = $("#inputPassword").val();
var dataString = 'email='+email+'&password='+password;
if($.trim(username).length>0 && $.trim(password).length>0)
{
$.ajax({
type: "POST",
url: "user/login.php",
data: dataString,
cache: false,
beforeSend: function(){ $("#entrar").val('A entrar...');},
success: function(data){
if(data)
{
showNotas();
$('#form-signin').fadeOut('slow', function(){
$("#section-1 .container").load("user/perfil.php").fadeIn(1500).delay(5000);
$("#section-1 span").text("Perfil");
});
$('#notas').show();
}
else
{
$("#entrar").val('Sign in')
$("#error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
}
}
});
}
return false;
});

编辑:Chrome 开发人员未显示任何错误

最佳答案

Chrome 开发者工具可能不会在您的控制台中显示任何错误,但是,请转到“网络”选项卡,单击“XHR 请求”(文件 AJAX 将 ping 来发出登录用户的请求,我假设它将是 user/login.php,然后转到“预览”和/或“响应”选项卡并查看回传他们对我们说的话。这很可能只是一个 PHP 错误,而不是 JavaScript

关于javascript - 使用 Ajax 登录 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31002528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com