Front-end small White's daily learning record----PHP (4) Ajax

Source: Internet
Author: User
Tags echo date

Ajax Technology (asynchronous request)
Local No refresh technology: Ajax technology does not need to refresh the page to get the results of the server,
So the experience is enhanced.

Request: Send network connection once
Purpose: To obtain data for the server

Steps:

The first step: To use AJAX technology, you have to get the Ajax object of the browser via the browser's built-in XMLHttpRequest to get the Ajax object Step Two: Opens the request open (the request is opened in the same way, the requested address, true) Step three: Send request sent using send data, get method generally send null, because the get way of data is passed through the URL address eg: using ajax to output a Hello world1. Create a new data.txt file in the WWW directory and add the content inside Hello World. 2. Create a new ajax.php in the same directory as the following code:3. Start Phpstudy4. Open the browser input localhpst/ajax.html5. Click the button to see the effect
<! DOCTYPE html>eg: output A current time with ajax+php1. Create a new php file under the WWW directory server.php   server.php
<?php    //Echo Date ("y-m-d h:i:s");    if (isset ($_get[' name '))) {  //isset determines whether the request exists    //     echo $_get[' name ']. '---> '. $_get[' age '). '---> '. Date ("y-m-d h:i:s");    }else if (isset ($_post[' name ')) {    //     echo $_post[' name ']. '---> '. $_post[' age '). '---> '. Date ("y-m-d h:i:s");     //Echo ' POST request: '. Date (' y-m-d h:i:s ');    }    Echo $_request[' name ']. '---> '. $_request[' age '). '---> '. Date ("y-m-d h:i:s");? >

2. Create a new php file under the WWW directory time.php<! DOCTYPE html>

}}//get method Xhr.open (' GET ', url, true);                Xhr.send (NULL);                   /*//post method Xhr.open (' POST ', url, true);                   If the data is sent by post, a request header Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ') needs to be added; Post data is placed in the Send function and sent past Xhr.send ("name=lxb&age=22"); */}} &L t;/script>

3. Open the browser input localhpst/time.html

4. Click the button to see the effect

The Ajax of jquery

$.get $.post $.ajax Three kinds of function forms

$.get ("1", {2}, 3);

1, Request URL 2, sent data (JSON format) 3, after successful callback function (parameter for received result-->res)

eg:$.get ("sever.php", {' province_id ',}, function (res) {});the classic Ajxa----"Three-level linkage1. Provincial and municipal forms2. Three methods of replicating table dataI. Data in the MySQL directory under the installation Phpstudy directory records the database informationtwo. Importing SQL files with database management softwarethree. Copying tables with database management software 3. The data returned by the database is usually in the form of a string or XML So after PHP gets the array result, we use Json_encode to convert the array into a string of similar format.
<?php    Echo Json_encode ($arr);? >
3. Sometimes the Ajax data is a string, to be converted into JSON form to use
<script>            /* JSON object                Parse: String->json object                Stringify:json Object--String        *//        var obj = {        //"     a": Ten,        //     "B": +        //};        Alert (typeof obj);        var res = json.stringify (obj);        Alert (res);        Alert (typeof res);        var str = ' {' A ': ten, ' B ': ';        Alert (typeof obj);        var obj = json.parse (str);        Alert (obj.a + ', ' + obj.b);</script>

  

Front-end small White's daily learning record----PHP (4) Ajax

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.