PHPAjax implements no additional comments on the page

Source: Internet
Author: User
PHPAjax allows users to post comments without refreshing pages. the traditional posting process is: Publishing-> submitting a page form-> waiting to refresh the page, in this way, when the network is crowded, it often takes a long time to wait. today we will introduce the use of PHP + Ajax to implement a new page without refreshing comments, hoping to help beginners with ajax PHPer.

First, we need a basic ajax development Framework The file ajax. js contains the framework. the code is as follows:

Var http_request = false;
Function send_request (url) {// initialization, specifying the processing function and sending the request function
Http_request = false;
// Start initializing the XMLHttpRequest object
If (window. XMLHttpRequest) {// Mozilla Browser
Http_request = new XMLHttpRequest ();
If (http_request.overrideMimeType) {// sets the MIME category
Http_request.overrideMimeType ("text/xml ");
}
}
Else if (window. ActiveXObject) {// IE browser
Try {
Http_request = new ActiveXObject ("Msxml2.XMLHttp ");
} Catch (e ){
Try {
Http_request = new ActiveXobject ("Microsoft. XMLHttp ");
} Catch (e ){}
}
}
If (! Http_request) {// exception. An error occurred while creating the object instance.
Window. alert ("An error occurred while creating the XMLHttp object! ");
Return false;
}
Http_request.onreadystatechange = processrequest;
// Determine the request sending method, URL, and whether to execute the following code synchronously
Http_request.open ("GET", url, true );
Http_request.send (null );
}
// Function for processing the returned information
Function processrequest (){
If (http_request.readyState = 4) {// judge the object status
If (http_request.status = 200) {// The message is returned successfully. start to process the information.
Document. getElementById (reobj). innerHTML = http_request.responseText;
}
Else {// The page is abnormal.
Alert ("the page you requested is abnormal! ");
}
}
}
Function checkfourm (obj ){
Var f = document. fourm;
Var newfourm = f. newfourm. value;
Var username = f. username. value;
Var id = f. id. value;
If (username = ""){
Document. getElementById (obj). innerHTML = "you must log on first! ";
Return false;
}
Else if (newfourm = ""){
Document. getElementById (obj). innerHTML = "You have not entered the comment! ";
Return false;
}
Else {
Document. getElementById (obj). innerHTML = "sending data ...";
Send_request ('sendnewfourm. php? Username = '+ username +' & newfourm = '+ newfourm +' & id = '+ id );
Reobj = obj;
}
}

A little ajax-based comments should be able to read this code. we can see that when we start to post comments, the code is displayed in a specific position: sending data .... Then call the callback function to process the data. Please refer to the server code:

Header ('content-Type: text/html; charset = GB2312 '); // avoid Chinese garbled characters. this is not required in linux.
$ Username = trim ($ _ GET ['username']);
$ Newfourm = trim ($ _ GET ['newfourm']);
$ Id = $ _ GET ['id'];
$ Time = date ("Y-m-d ");

Include ('Inc/config. inc. php ');
Include ('Inc/dbclass. php ');
$ Db = new db; // generates an instance from the database operation class
$ Db-> mysql ($ dbhost, $ dbuser, $ dbpassword, $ dbname); // call the connection parameter Function
$ Db-> createcon (); // call to create a connection function

$ Addsql = "insert into cr_fourm values (0, '$ newfourm',' $ username', '$ time', $ id )";
$ Db-> query ($ addsql );
Echo "the comment has been published successfully! ";
// Echo $ addsql;
$ Db-> close (); // close the database connection
?>

Because jsvascript uses UTF8 encoding, the returned information of the ajax return server in windows appears. Garbled Therefore, it is necessary to start the first sentence of the application in win. The two files in the middle contain database operations and database configuration information. I am used to writing basic database operations into a class for convenient calling. I believe that you have basically understood the working principle of this program. The HTML code on the page is provided:



















This is part of my web page, that is, the framework for implementing this function. Code , Display Comment Of Page Call with IFRAME (hidden frame). after the message is sent, you can only refresh the IFRAME to view your comments. you do not need to refresh the entire page throughout the process. Now, let's take a look! Pai_^

1. click "submit" to start sending data

2. data is successfully sent.

3. refresh the comment list

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.