Without talking nonsense, go directly to the key code:
JS Code:
View code
<Script language = "JavaScript">
$ (". Login"). Live ('click', function (){
VaR username = $ (". input_user"). Val ();
VaR Password = $ (". input_ps"). Val ();
If (username = ""){
Alert ("user name cannot be blank ");
Return false;
}
If (Password = ""){
Alert ("the password cannot be blank ");
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 ("incorrect user name or password. Please check and log on again! ");
Window. Location = "login. php ";
}
});
});
</SCRIPT>
Checklogin. php
View code
<? 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 "Login Failed. Please enter the correct user name and password! ";
} 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 );
}
}
?>
The main tasks are as follows:
At the end of checklogin. php, the code should be written as follows:Echo json_encode ($ admininfo );
Previously written as: json_encode ($ admininfo); the result is printed as JSON. username is always undifined.Alert ....