Yesterday study Ajax, want to make a landing box, no refresh on the user name password submitted to the background PHP program, verify and send back.
After a while, I finally came out. Front Code:
<script src= "./javascript/jquery-latest.pack.js" type= "Text/javascript" ></script> //import jquery Framework File You have to do this with jquery.
<script type= "Text/javascript" >
$ (document). Ready (function () {//This is Jqueryready, which is like the C language of main all operations contained within it
$ ("#button_login"). MouseDown (function () {
Login (); //Click on the button with the id "button_login" to trigger the function login ();
});
});
function login () { //functions login ();
var username = $ ("#username"). Val (); user name in//Take box
var password = $ ("#password1"). Val (); //Take the password in the box
$.ajax ({An AJAX process
Type: "Post",//Post to the background communication
URL: "./php_ajax/login.php",//communicate with this PHP page
DataType: ' JSON ',//The value returned from PHP is interpreted in JSON
Data: ' username= ' +username+ ' &password= ' +password,There are two items of data sent to PHP, the U and P, respectively.
Success:function (JSON) {//If PHP is successfully invoked
Alert (json.username+ ' \ n ' +json.password); //Put the return value (json.username) in PHP to alert.
}
});
}
</script>
The specific PHP I just return the code intact, it should be changed to a database check.
<?php
echo json_encode (Array (' username ' =>$_request[' username '], ' password ' =>$_request[' password '));
?>
The function of Json_encode is to wrap the variables in PHP into a JSON format and echo it back to the Jquery.ajax () that called it.
In general, the process is:
User fill out user name password click the button "Login"
JQuery gets user name password
jquery sends two variables to login.php by post
Login.php handles the variables and encapsulates the return as JSON
Throw JSON to the front page
The foreground interprets the resulting JSON data and processes the next step.
About JSON is a kind of data encapsulation, similar to XML but more suitable for lightweight applications.
jquery+ajax+php Study Notes