Comment on a page without refresh with PHP Ajax
everyone has the experience of comment on the website, the traditional publishing process is no more: publish - G; submit a page form - G; wait brush New pages, so when the network is more crowded, it often needs a long wait, today, the introduction of PHP Ajax pages without refresh to publish comments, hope to learn the initial Ajax PHPer help. So first, we need a basic ajax development framework, and the file ajax.js contains the framework, the code as follows:
[code]
var http_request=false;
function send_request (URL) / / / / / / / / / / / / / / / / / / / / / / / / > = > > =
/ > = > >
/ > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / > / / initializes the initialization of the XMLHttpRequest object
if (window.XMLHttpRequest) //Mozilla browser
{
http_request=new XMLHttpRequest ();
if (http_request.overrideMimeType) / > > > > >; );
}
}
else if (window.ActiveXObject) //IE browser
{
try
{
http_request=new / > {> > > > > > > > R / > http_request=new ActiveXobject (.Quot; Microsoft.XMLHttp.quot;);
}catch (E)
{
}
}
> / > exception. Like failure! .quot;);
return false;
}
http_request.onreadystatechange=processrequest;
http_request.onreadystatechange=processrequest;
http_request.open (.Quot; return)
function processrequest ()
function checkfourm (obj)
{
var f=document.fourm; LementById (obj).InnerHTML=.quot;.Lt; img src=images/false.gif.gt;.Lt; font color=red.gt; you must log in first! .lt; /font.gt;.Quot;
return false;
}
else if (newfourm==.quot;.Quot;). .lt; /font.gt;.Quot;
return false;
}
else
{
document.getElementById (obj); R / >}
}
[/code]
there is a bit of Ajax based through annotation that should be able to understand this code, and we can see that when we begin to publish a comment, it is displayed at a specific location: the data is being sent... The callback function is then invoked to process the data. Then look at the server-side code:
[code].lt;? Php
header ('Content-Type:text/html; charset=GB2312'); / / / / / / avoid the output of Chinese chaos code, Linux does not need
$username=trim ($_GET.#91;'username'.#93;); #93;;
$time=date (.Quot; Y-m-d.quot;);
include ('inc/config.inc.php');
include ('inc/dbclass.php'); / / / / / / / / / / / / / / / / invoke connection parameter function; ) / / / / / call to create a connection function
$addsql=.quot; insert into cr_fourm values (0,'$newfourm','$username','$time', $id).Quot; .lt; /font.gt;.Quot;
//echo $addsql;
$db-.gt; close (); / / / / / / / / /.Gt; [/code]
there will be chaos in the return message of the server. The two contained file in the middle is the database operation class and the database configuration information. I am personally used to write the basic database operations into a class, which is convenient to call. It is believed that we have basically understood the working principles of the program, and in giving the HTML code of the page:
[code].lt; table width=.quot; 100%.quot; border=.quot; 0.quot; cellspacing=.quot; 0.quot; cellpadding=.quot; O $rows_p.#91; p_info.#93;.Gt;.Lt; /td.gt;
.Lt; /tr.gt;
.Lt. : inherit; WIDTH:
98%; Z-INDEX:2.Gt;.Lt; /iframe.gt;
.Lt; /td.gt;
.Lt. Ot;.Gt;
.Lt; table width=.quot; 100%.quot; border=.quot; 0.quot; cellspacing=.quot; 0.quot; cellpadding=.quot. Lu) Username:
.Lt; input name=.quot; username.quot; type=.quot; text.quot; value=.quot;.Lt; =$username?.gt;.Quot; 32.quot; align=.quot; center.quot; valign=.quot; middle.quot;.Gt;.Lt; textarea name=.quot; newfourm.quot; class=.quot; f.quot; dialectical; dialectical; .lt; input name=.quot; submit.quot; type=.quot; button.quot; value=.quot; published commentary.Quot; onClick=.quot; checkfourm ('result').Quot; E=.quot; id.quot; type=.quot; hidden.quot; id=.quot; id.quot; value=.quot;.Lt; PHP echo.quot; $id.quot;.Gt; plurality; > R / > this is part of my web page, that is, the framework code to realize this function. It shows that the page of the review is called with IFRAME (hidden frame). After the message is sent out, only the piece of IFRAME can be refreshed to see its own comments, from sending to view, the whole process does not need to refresh the whole page.