One-minute Ajax sample of how to implement the suggest Function

Source: Internet
Author: User

Ajax is not a fresh technology. In MSDN, which Microsoft launched in, it introduced the function of msxml and xmlhttpreuqest to remotely read xml documents. However, at that time, the B/S technology was far less popular than today, so ajax was turned out with a nice name and then hyped again.

There are many ajax implementation technologies. Of course, I am referring to the encapsulation method. Here I use an ajax framework that I think is the most reasonable: xajax

Next we will look at how to complete a google suggest application with simple code.

1. First, create an HTML file and copy it in it. Of course, you do not need to write this code. Just copy it.


Include_once ("server1.server. php"); # servidor para XAJAX
$ Xajax-> printJavascript (); // This is the key, so the function is automatically generated here.
?>

Next, we create


 

Here, when onkeydown, we obtain the corresponding suggest based on the user input.

2 In ejemplo2.js

Var inter = null;
// Call the functions in php
Function buscar (){
If (inter) clearInterval (inter );
Inter = setInterval ("xajax_busc (document. getElementById ('cfdd'). value); clearInterval (inter);", 300 );
Gsc_show (document. getElementById ('search-results '));
}

Function gsc_show (elt)
{
If (elt) elt. style. display = 'block ';
}

Function gsc_hide (elt)
{
If (elt) elt. style. display = 'none ';
}

3 In server1.server. php

// This function is used to query suggest. Here we use an array. In actual use, this array is obtained based on DB queries.

Function busc ($ bu ){
If ($ bu! = "") | ($ Bu = "")){
$ ObjResponse = new xajaxResponse ();
$ Cfdds = array ("master data center", "Network Office", "Small Data Center", "20 research buildings ");
$ Sa = "";

Foreach ($ cfdds as $ cfdd ){
$ Sa. ="

. "Onclick =/" javascript: document. getElementById ('cfdd '). value = '$ cfdd'; gsc_hide (document. getElementById ('search-results '));/""
. "> $ Cfdd

";
}
$ ObjResponse-> addAssign ("search-results", "innerHTML", $ sa );
Return $ objResponse-> getXML ();
}
Else {
$ ObjResponse = new xajaxResponse ();
$ ObjResponse-> addAssign ("search-results", "innerHTML ","");
Return $ objResponse-> getXML ();
}
}

// Register this function

$ Xajax = new xajax ("server1.server. php", "xajax _", false );
$ Xajax-> registerFunction ("busc ");

In this way, the suggest function can be implemented. You must think it strange that since ajax is used, neither xmlhttprequest nor xml parsing is called, because these operations are encapsulated by xajax.

In fact, the purpose of ajax operations is html-> javascript-> xmlhttprequest-> php-> xml-> javascript-> html

Now we only need HTML-> JavaScript ....... -> PHP-> ...... -> ...... -> in HTML, three of them are hidden. It is very simple for users.

I will also demonstrate how to extend the xajax implementation and automatically read the cascade select sample.

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 508265

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.