废话不说直接上关键代码:
js代码:
<script language="javascript"> $(".login").live('click',function(){ var username=$(".input_user").val(); var password=$(".input_ps").val(); if(username==""){ alert("用户名不能为空"); return false; } if(password==""){ alert("密码不能为空"); return false; } var URL="checkLogin.php?"; var DATA="&username="+username+"&password="+password; $.getJSON(URL+DATA,function(json){ if(json.username=='true_u'&&json.password=='true_p'){ //alert(json.username+"|"+username+'...1'); window.location="index.php"; } if(json.username=='error_u'||json.password=='error_p'){ alert("用户名输入或密码输入有误,请检查后重新登陆!"); window.location="login.php"; } }); }); </script>
checkLogin.php
<?php
include_once('connect.php'); $username=$_GET['username']; $password=MD5($_GET['password']); //echo $username.'###'.$password; $sql="select * from admin"; $result=mysql_query($sql); if(!$result){ mysql_close(); echo "登陆失败,请输入正确的用户名和密码!"; }else{ while($row=mysql_fetch_array($result)){ if($row['username']==$username&&$row['password']==$password){ session_start(); $_SESSION['username']=$row['username']; $adminInfo['username']='true_u'; $adminInfo['password']='true_p'; mysql_close(); }else if($row['username']!=$username){ $adminInfo['username']='error_u'; } if($row['password']!=$password){ $adminInfo['password']='error_p'; } //var_dump($adminInfo);exit; echo json_encode($adminInfo); } } ?>
其中主要被干倒的地方如下:
在checkLogin.php最后,代码应该这么写:echo json_encode($adminInfo);
之前写成:json_encode($adminInfo);结果打印 json.username老是undifined,以此为警戒....