Simple use of AJAX in PHP-PHP source code

Source: Internet
Author: User
Ec (2); [AJAX Introduction] Ajax is a Web application development method that uses client scripts to exchange data with Web servers. Web pages can be dynamically updated without interrupting the interaction process and cutting them again. With Ajax, users can create direct, highly available, richer, and more dynamic Web user interfaces close to local desktop applications. Asynchronous merge Cr merge pt and XML (AJAX) are not new technologies. Instead, they use several existing technologies & mdash; including Cascading Style Sheets (script ec (2 ); script




[AJAX Introduction]

Ajax is a Web application development method that uses client scripts to exchange data with Web servers. Web pages can be dynamically updated without interrupting the interaction process and cutting them again. With Ajax, users can create direct, highly available, richer, and more dynamic Web user interfaces close to local desktop applications.

Asynchronous Cr ī pt and XML (AJAX) are not new technologies, but they use several existing technologies-including Cascading Style Sheets (CSS), Javascr ī pt, XHTML, XML, and extensible style language conversion (XSLT), develop Web application software that looks and operates similar to desktop software.


[AJAX execution principle]

An Ajax interaction starts from an object called XMLHttpRequest's Javascr into pt. As the name implies, it allows a client script to execute an HTTP request and parse a server response in XML format. The first step in Ajax processing is to create an XMLHttpRequest instance. Use the HTTP method (GET or POST) to process the request and set the target URL to the XMLHttpRequest object.

When you send an HTTP request, you do not want the browser to suspend and wait for the response from the server. Instead, you want to continue responding to user interface interactions through the page, and post-processing them after the server response actually arrives. To complete this, you can register a callback function with XMLHttpRequest and asynchronously dispatch the XMLHttpRequest request. The Control Right is immediately returned to the browser. When the server responds, the callback function will be called.


[AJAX application]


1. initialize Ajax

Ajax actually calls the XMLHttpRequest object. First, we must call this object. We construct an Ajax initialization function:

/**
* Initialize an xmlhttp object
*/
Function InitAjax ()
{
Var ajax = false;
Try {
Ajax = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
Ajax = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (E ){
Ajax = false;
}
}
If (! Ajax & typeof XMLHttpRequest! = ''Undefined ''){
Ajax = new XMLHttpRequest ();
}
Return ajax;
}

You may say that because the XMLHTTP component is called by the IE browser, this code can be used only by Firefox.
Before executing any Ajax operation, we must call our InitAjax () function to instantiate an Ajax object.


2. Use the Get Method

Now let's first execute a Get request. Do we need to Get/show. php? Id = 1 data, so what should we do?
Suppose there is a link: News 1. When I click this link, I don't want to refresh it to see the link content. What should we do?

// Change the link:
News 1

// Set a layer for receiving news, and set it to not display:



At the same time, construct the corresponding Javascr ī pt function:

Function getNews (newsID)
{
// If newsID is not passed in
If (typeof (newsID) = ''undefined '')
{
Return false;
}
// URL for Ajax
Var url = "/show. php? Id = "+ newsID;

// Obtain the position of the news display Layer
Var show = document. getElementById ("show_news ");

// Instantiate an Ajax object
Var ajax = InitAjax ();

// Get requests
Ajax. open ("GET", url, true );

// Obtain the execution status
Ajax. onreadystatechange = function (){
// If the execution is normal,

<

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.