How to Use ajax-examples, ajax data processing, and ajax Data Processing

Source: Internet
Author: User

How to Use ajax-examples, ajax data processing, and ajax Data Processing

It should be noted that the encapsulated database called and the Save address of jQuery

I. Registration

(1) Write a text box to verify the user name

<Input type = "text" id = "uid"/> <span id = "tishi"> </span> //

(2) write with jQuery statement: the user name is unavailable, and the user name can be used

$ ("# Uid "). blur (function () {// obtain the user name var uid = $ (this ). val (); // query the database and call ajax $. ajax ({// url in the ajax method must be in json format: "zhucecl. php ", // process the page address data: {u: uid}, // here is the Json format: u is the starting name, uid is the value type: "POST", // data submission method dataType: "TEXT", // returned data format: string format success: function (data) {// if successful, return the anonymous function (callback function) // The statement var str = ""; if (data = "OK") after the processing page is successfully executed ") {str = "user name can be used"; $ ("# tishi" ).css ("color", "green");} else {str = "existing user name "; $ ("# tishi" ).css ("color", "red") ;}$ ("# tishi "). text (str );}});})

(3) Registration processing page: I wrote it many times and I will not explain it again.

<? Php // call the encapsulated class: Pay attention to the storage location include ("DBDA. class. php "); $ db = new DBDA (); // name starting from the passed value $ uid = $ _ POST [" u "]; $ SQL = "select count (*) from renyuan where username = '{$ uid }'"; // call the encapsulated function $ attr = $ db-> Query ($ SQL); // determine whether the user name exists if ($ attr [0] [0]> 0) {echo "NO"; // There are duplicate usernames} else {echo "OK"; // NO duplicate usernames}?>

Ii. Logon

(1) write text box and login button

<Div> account: <input type = "text" id = "uid"/> </div> <div> password: <input type = "password" id = "pwd"/> </div> <input type = "button" value = "login" id = "btn"/>

(2) Write jQuery statements 

$ ("# Btn "). click (function () {var uid = $ ("# uid "). val (); // find the user var pwd =$ ("# pwd "). val (); // find the password // call the ajax method, which must be in json format $. ajax ({url: "denglucl. php ", // data: {uid: uid, pwd: pwd}, type:" POST ", dataType:" text ", success: function (data) {if (data. trim () = "OK") {window. location. href = "zhuce. php "rel =" external nofollow "; // enter the correct username and password.} else {alert (" Incorrect username and password ");}}})})

(3) login processing page: I wrote it many times and I will not explain it again

<? Phpinclude ("DBDA. class. php "); $ db = new DBDA (); $ uid = $ _ POST [" uid "]; $ pwd = $ _ POST [" pwd "]; $ SQL = "select mima from huiyuan where yonghu = '{$ uid}'"; $ attr = $ db-> Query ($ SQL); if (! Empty ($ pwd )&&! Empty ($ attr) & $ attr [0] [0] = $ pwd) // The password is not empty, and the array is not empty, whether the password is the same as the Password Found {echo "OK" ;}else {echo "ON ";}

  

Logon successful:

Iii. ajax Data Processing

(1) first display the name line. You can edit it normally.

<Table width = "100%" border = "1" cellpadding = "0" cellspacing = "0"> <tr> <td> Code </td> <td> name </ td> <td> price </td> <td> origin </td> <td> Inventory </td> <td> operation </td> </tr> </ table>

(2) write another table with the displayed content

<Tbody id = "bg"> // contains the data in a table that is traversed. </tbody>

(3) Write jQuery. After the page is loaded, execute

$ (Document ). ready (function (e) {$. ajax ({url: "xianshicl. php ", dataType:" TEXT ", success: function (data) {// executed after page processing is complete });

(4) Compile the display data processing page (two methods)

Include ("DBDA. class. php "); // call encapsulated classes: Note the location $ db = new DBDA (); // create a new object $ SQL =" select * from fruit "; // write the SQL statement echo $ db-> StrQuery ($ SQL); // 1. call the encapsulated concatenation array as a string/* // 2. $ attr = $ db-> Query ($ SQL); // call the method in the encapsulation class to execute the SQL statement $ str = ""; foreach ($ attr as $ v) {$ str. = implode ("^", $ v ). "|"; // concatenate an array as a string} echo substr ($ str, 0, strlen ($ str)-1); // intercept the string: the last concatenation operator is not displayed <br> */

(5) After the processing page ends, write the statement after the processing page ends in the ajax success method on the home page

Success: function (data) {var hang = data. split ("|"); // split character "|" string: Display line var str = ""; for (var I = 0; I 

This shows:

The above example about how to use ajax and how to process ajax data is all the content that I have shared with you. I hope to give you a reference, and I hope you can provide more support to the customer center.

Related Article

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.