Php AJAX登录表单-单击submit(';行不通
我正在尝试创建自己的登录表单,但有一些困难 我想要一个用户可以使用AJAX登录网站的表单:Php AJAX登录表单-单击submit(';行不通,php,ajax,forms,login,pdo,Php,Ajax,Forms,Login,Pdo,我正在尝试创建自己的登录表单,但有一些困难 我想要一个用户可以使用AJAX登录网站的表单: 如果用户未输入用户名,则显示消息 如果用户未输入密码,则显示消息 检查数据库密码是否正确,如果不正确,显示消息 问题是,我以前做过一点jQuery,几乎没有AJAX。我正在努力学习,但到目前为止还没有任何运气!我的PHP/PDO脚本在没有任何AJAX/jQuery的情况下运行良好,并且可以很好地检查所有需求 到目前为止,我的代码如下: index.html $(文档).ready(函数(){ $(“#a
$(文档).ready(函数(){
$(“#add_err”).css('display','none','important');
$(“#登录提交”)。单击(函数(){
用户名=$(“#用户名”).val();
密码=$(“#密码”).val();
如果(username.length<1)
errors=errors+“请输入您的用户名
”;
if(password.length<1)
错误=错误+“请输入您的密码
”;
var误差=”;
$.ajax({
类型:“POST”,
url:“process login.php”,
数据:“username=“+username+”&password=“+password,
成功:函数(html){
如果(html=='true'){
window.location=“account.php”;
}
如果(错误!=“”){
$(“添加错误”).html(错误).slideDown(“快速”);
}
}
});
返回false;
});
});
是否有什么地方我遗漏了或做得完全错误?首先,您应该检查php脚本是否返回您在JS端测试的正确值 然后您应该检查以下内容:
if(html=='true') {
window.location="account.php";
}
if (errors != "") {
$("add_err").html(errors).slideDown("fast");
}
我认为你应该做到:
if(html=='true') {
window.location="account.php";
}else{
errors = html ;//when 'html' variable is not returning 'true' the its returning errors
}
if (errors != "") {
$("add_err").html(errors).slideDown("fast");
}
我希望能为我们提供更多关于这个问题的信息,比如您测试它时显示了什么。Hmm,没有出现错误。我猜是我的流程文件有问题。-我认为我需要返回true/false是否正确。是的,如果凭据正确,则应返回true,否则返回错误以显示它。或者,如果错误仅限于js文件,则返回false。在这种情况下,问题在于php方面。php代码给了我“成功”。在我注释了标题行之后。这可能是问题所在,请对其进行注释。并将javascript错误“”始终更改。将此if(html=='true')替换为if(html.trim()=='Success')
if(html=='true') {
window.location="account.php";
}
if (errors != "") {
$("add_err").html(errors).slideDown("fast");
}
if(html=='true') {
window.location="account.php";
}else{
errors = html ;//when 'html' variable is not returning 'true' the its returning errors
}
if (errors != "") {
$("add_err").html(errors).slideDown("fast");
}