jquery+ajax+php Study Notes

Source: Internet
Author: User

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

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.