PHP AJAX Implementation Page No refresh comment _php Tutorial

Source: Internet
Author: User
Everyone has the experience of commenting on the site, the traditional publishing process is nothing more than: publishing, submit page form, and waiting to refresh the page, so when the network is crowded, often need a long wait, today introduced with Php+ajax implementation page no refresh comment, Hope that the phper of the beginner Ajax help.

So first of all, we need a basic AJAX development Framework , the file Ajax.js contains this framework, the code is as follows:

var Http_request=false;
function send_request (URL) {//Initialize, specify handler function, send request functions
Http_request=false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest) {//mozilla Browser
Http_request=new XMLHttpRequest ();
if (http_request.overridemimetype) {//Set 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, creation of object instance failed
Window.alert ("Failed to create XMLHTTP Object! ");
return false;
}
Http_request.onreadystatechange=processrequest;
Determine how to send the request, URL, and whether to synchronize execution of the next snippet of code
Http_request.open ("GET", url,true);
Http_request.send (NULL);
}
function to process return information
function ProcessRequest () {
if (http_request.readystate==4) {//Judging object state
if (http_request.status==200) {//information has been successfully returned to start processing information
document.getElementById (reobj). Innerhtml=http_request.responsetext;
}
else{//page is not working
Alert ("The page you requested is not working!") ");
}
}
}
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 in first!" ";
return false;
}
else if (newfourm== "") {
document.getElementById (obj). innerhtml= "You haven't filled in the comments yet! ";
return false;
}
else{
document.getElementById (obj). innerhtml= "Sending data ...";
Send_request (' sendnewfourm.php?username= ' +username+ ' &newfourm= ' +newfourm+ ' &id= ' +id ');
Reobj=obj;
}
}

There is a bit of Ajax based on the annotation, should be able to read this code, we can see, when we start to comment, in a specific place first display: sending data .... The callback function is then called to process the data. So, look at the server-side code:

Header (' content-type:text/html;charset=gb2312 ');//Avoid the output of Chinese garbled, Linux does not need
$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;//to generate an instance from a 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 "Comment has been successfully published! ";
Echo $addsql;
$db->close ();//Close database connection
?>

Because Jsvascript uses UTF8 encoding, the return information of AJAX loopback server under Windows will be garbled , so it is necessary to apply the first sentence in win. The middle part of the two include files are database operation class and database configuration information, I personally used to write basic database operations a class, convenient to call. Here I believe you have a basic understanding of how this program works, in the HTML code to give the page:



















This is part of my web page, that is, the framework code to implement this function, the page that shows the comment is called with an IFRAME (hidden frame), and after the message is sent, only the one piece of the IFRAME can be seen from Comments, from Send to view, the whole process does not need to refresh the entire page. All right, let's take a look at it! ^_^

1. Click "Submit" to start sending data

2. Successful data transmission

3. Refresh the list of comments

http://www.bkjia.com/PHPjc/317711.html www.bkjia.com true http://www.bkjia.com/PHPjc/317711.html techarticle Everyone has the experience of commenting on the website, the traditional publishing process is nothing more than: publish-submit page form-Wait to refresh the page, so when the network is crowded, often need diffuse ...

  • 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.