PHP and JSON data transmission (without refreshing)

Source: Internet
Author: User

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 ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.