PHP No refresh post Comment implementation code

Source: Internet
Author: User

Let's take a look at the effect map, this is part of my web page, that is, to implement this function of the framework code, display the comments page with an IFRAME (hidden frame) call, after the message sent, only to refresh the IFRAME that piece can see their own comments, from Send to view, The whole process does not need to refresh the entire page.

1. Click "Submit" to start sending the data



2. Data sent successfully

3. Refresh the comments list

Okay, now let's start with the code.

The code is as follows Copy Code

var Http_request=false;
function send_request (URL) {//initialization, specifying handler functions, sending requested 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, failed to create object instance
Window.alert ("Create XMLHTTP object failed!") ");
return false;
}
Http_request.onreadystatechange=processrequest;
Determine how to send the request, the URL, and whether to execute the next code synchronously
Http_request.open ("Get", url,true);
Http_request.send (NULL);
}
Functions that process return information
function ProcessRequest () {
if (http_request.readystate==4) {//Judge object state
if (http_request.status==200) {//information returned successfully, start processing information
document.getElementById (reobj). Innerhtml=http_request.responsetext;
}
else{//page is not normal
Alert ("The page you requested is not normal!") ");
}
}
}
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= " <font color=red> You must log in first! </font> ";
return false;
}
else if (newfourm== "") {
document.getElementById (obj). innerhtml= " <font color=red> You have not filled in the comments! </font> ";
return false;
}
else{
document.getElementById (obj). innerhtml= "Sending data ...";
Send_request (' sendnewfourm.php?username= ' +username+ ' &newfourm= ' +newfourm+ ' &id= ');
Reobj=obj;
}
}

There is a little Ajax based on the annotation, you should be able to understand this code, we can see that when we start to comment, in a specific location to show first: sending data .... The callback function is then invoked to process the data. Look at the server-side code, then.

  code is as follows copy code
<?php
  Header (' content-type:text/html;charset=gb2312 ');/to avoid the output of Chinese garbled, Linux does not require the
  $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 operations class
  $DB->mysql ($dbhost, $dbuser, $dbpassword, $dbname);//Call connection parameter function
  $DB-> Createcon ()//Call Create Connection function
 
  $addsql = INSERT INTO Cr_fourm values (0, ' $newfourm ', ' $username ', ' $time ', $id) ";
  $db->query ($addsql);
  echo <font color=red> Comment has been published successfully! </font> ";
 //echo $addsql;
  $DB->close ();//Close database connection

Because Jsvascript adopts UTF8 code, the return information of AJAX loopback server will appear garbled under Windows, so it is very necessary to apply the first sentence in win. The middle section two contains files are database operation class and database configuration information, I am accustomed to the basic database operations written as a class, easy to invoke. Here I believe that we have a basic understanding of how the program works, in the given page of the HTML code

The code is as follows Copy Code

<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" >
<tr>
&LT;TD align= "center" ><?php Echo $rows _p[p_info];? ></td>
</tr>
<tr>
&LT;TD align= "center" ><br><br><iframe frameborder= "0" scrolling= "Auto" src= "showfourm.php?picid=" <?= $id;? > "style=height:250px; Visibility:inherit; width:98%; Z-index:2 ></iframe>
</td>
</tr>
<tr>
&LT;TD align= "center" ><br><br>
<div align= "center" id= "result" ></div>
<form name= "Fourm" >
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" >
<tr>
&LT;TD height= > Express Comment <span class= "STYLE1" > (must first login) Username:
<input name= "username" type= "text" value= "<?= $username?>" readonly>
</span></td>
</tr>
<tr>
&LT;TD height= "align=" "Center" valign= "middle" ><textarea name= "Newfourm" class= "F" id= "Newfourm" ></ Textarea></td>
</tr>
<tr>
&LT;TD height= "> <input name=" Submit "type=" button "value=" comment "onclick=" Checkfourm (' result ') >
<input name= "reset" type= "reset" id= "reset" value= "re-fill" >
<input name= "id" type= "hidden id=" id "value=" <?php echo "$id";? > "></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

Php+ajax implementation of the page without a refresh comment, I hope to beginners Ajax phper help. We need a basic AJAX development framework, file Ajax.js contains this framework, ajax.js file we have the first step, as long as we follow the above method step-by-step operation can be achieved without a refresh comment

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.