Jquery AJAX without refreshing comments

Source: Internet
Author: User

This article uses asp and jquery to implement the Code without refreshing comments. If you want ajax to load comments and send comments, download them.

This article is based on asp and jquery.

<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312"/>
<Title> jquery ajax without refreshing comments </title>
<Script type = "text/Webpage effects" src = "jquery. webpage effects"> </script>
<Script type = "text/javascript">
Function stopbutton (){
Document. getelementbyid (arguments [0]). disabled = true;
Document. getelementbyid (arguments [0]). value = "Submit (" + arguments [1] + ")";
If (-- arguments [1]> 0 ){
Window. settimeout ("stopbutton ('" + arguments [0] + "'," + arguments [1] + ")", 1000 );
}
If (arguments [1] <= 0 ){
Document. getelementbyid (arguments [0]). value = 'Submit ';
Document. getelementbyid (arguments [0]). disabled = false;
}
}
Function getcomment ($ id, $ page ){
$. Ajax ({
Url: "comment. asp? Action = ajax_getcomment & id = "+ $ id +" & page = "+ $ page +" & time "+ new date (). tostring (),
Type: 'get ',
Success: function (){
Parameters ('{comment'}.html (arguments [0]);
}
});
}
Function sendcomment (){
Var $ commentparentid = arguments [0];
Var $ commentuser = $ ('# commentuser'). val ();
Var $ commenttext = $ ('# commenttext'). val ();
Var $ commentvalidate = $ ('# commentvalidate'). val ();
If ($. trim ($ commentuser) = '')
{
Alert ('enter your nickname! ');
$ ('# Commentuser'). focus ();
Return false;
}
If ($. trim ($ commentvalidate) = '')
{
Alert ('enter the verification code! ');
$ ('# Commentvalidate'). focus ();
Return false;
}
If ($. trim ($ commenttext) = '')
{
Alert ('enter the reply content! ');
$ ('# Commenttext'). focus ();
Return false;
}
If ($ commenttext. length <5 ||$ commenttext. length> 200)
{
Alert ('content must be between 5-characters! ');
Return false;
}
Stopbutton ('commentsubmit ', 10 );
$. Ajax ({
Url: "comment. asp? Action = ajax_sendcomment & commentparentid = "+ $ commentparentid +" & commentuser = "+ escape ($ commentuser) +" & commenttext = "+ escape ($ commenttext) + "& commentvalidate =" + escape ($ commentvalidate) + "& time =" + new date (). tostring (),
Type: 'get ',
Success: function (){
If (arguments [0] = 'error ')
{
Alert ('verification code error! ');
} Else {
Getcomment ($ commentparentid, 0 );
Alert (arguments [0]);
$ ("# Commenttext"). val ("");
// Refresh the verification code image when the verification is successful
$ ("# Commentvalidateimages"). attr ("src", "code. asp? S = "+ math. random ());
}
$ ("# Commentvalidate"). val ("");
}
});
// Setcookie ("commentuser", $ commentuser, 3 );
} </Script>
<Link rel = "stylesheet" type = "text/css tutorial" href = "style.css"/>
</Head>
<Body>
<Div class = "box">
<Div class = "box-title"> comments </div>
<Div id = "comment"> </div>
<! -- The comment input box starts -->
<Div style = "padding: 4px"> </div>
<Div class = "post">
<Div style = "padding: 4px"> </div>
<Label> & nbsp; & nbsp; Name (*) </label> <input type = "text" id = "commentuser" onfocus = "this. classname = 'focus postvalidate' "onblur =" this. classname = 'blur postvalidate' "class =" blur postuser "value =" anonymous user "/>
<Div style = "padding: 2px"> </div>
<Label> & nbsp; Verification Code (*) </label> <input type = "text" class = "blur postvalidate" id = "commentvalidate" onfocus = "this. classname = 'focus postvalidate' "onblur =" this. classname = 'blur postvalidate' "maxlength =" 4 "/>
& Nbsp;
<Div style = "padding: 2px"> </div>
<Label> & nbsp; Content & nbsp; content (*) </label> <textarea id = "commenttext" onfocus = "this. classname = 'focus posttext' "onblur =" this. classname = 'blur posttext' "class =" blur posttext "> </textarea>
<Div style = "padding: 2px"> </div>
<Span style = "padding-left: 60px;"> & nbsp; </span> <input type = 'button 'class = "postsubmit" id = "commentsubmit" value = 'sube' onclick = "sendcomment (1)"/>
<Div style = "padding: 2px"> </div>
</Div>
<! -- End the comment input box -->
</Div>
<Script type = "text/javascript">
// <! [Cdata [
Getcomment (1, 1 );
//]>
</Script>
</Body>
</Html>

<%
<! -- # Include file = "c. asp" -->
<%
Dim action, id, page, pagehtml, pages, linkhtml
Pages = 5
Action = request. querystring ("action ")
Id = request. querystring ("id ")
Page = request. querystring ("page ")
Select case action
Case "ajax_getcomment"
Ajax_getcomment ()
Case "ajax_sendcomment"
Ajax_sendcomment ()
Case else
Echo ("erro ")
End select

Function ajax_getcomment ()
Dim temlist, getlist
Openconn (dbpath)
SQL = "select commentuser, commentdate, commenttext, commentreply from comment where commentparentid =" & id & "order by commentid asc"
Call openrs (SQL, 1)
If not (rs. bof and rs. eof) then
Rs. pagesize = pages
Rscount = rs. recordcount
Pagenum = rs. pagecount
Page = clng (page)
If page <1 then page = 1
If page> pagenum then page = pagenum
Rs. absolutepage = page
K = 1
Do while not rs. eof
Temlist = "<div class =" "comment" ">" & vbcrlf
Temlist = temlist & "<div class =" "comment-caption" "> <span class =" "comment-user" ">" & rs (0) & "posted on </span> <span class =" "comment-date"> "& rs (1) &" </span> </div> "& vbcrlf
Temlist = temlist & "<div class =" "comment-text" ">" & rs (2) & "</div>" & vbcrlf
If rs (3) <> "then
Temlist = temlist & "<div class =" "comment-reply" "> reply:" & rs (3) & "</div>" & vbcrlf
End if
Temlist = temlist & "</div>" & vbcrlf
K = k + 1
Rs. movenext
Getlist = getlist & temlist
If k> pages then exit do
Loop
Else
Echo ("no comment ")
End if
Closers ()
Closeconn ()
'Display page-related information
If rscount> 0 then
Pagehtml = "<div class =" "pagebar"> existing "& rscount &" Comment & nbsp; {$ link_list $} </div>"
If pagenum> 1 then
For I = 1 to pagenum
Linkhtml = linkhtml & iif (I = page, "<span class =" "current"> "& I &" </span> & nbsp ;", "<a href =" "javascript: void (0)" "onclick =" "getcomment (" & id & "," & I &") ""> "& I &" </a> & nbsp ;")
Next
Else
Linkhtml = ""
End if
Pagehtml = replace (pagehtml, "{$ link_list $}", linkhtml)
End if
Echo (getlist & pagehtml)
End function

Function ajax_sendcomment ()
Dim commentparentid, commentuser, commenttext, commentvalidate
Openconn (dbpath)
Commentparentid = request. querystring ("commentparentid ")
Commentuser = unescape (request. querystring ("commentuser "))
Commenttext = unescape (request. querystring ("commenttext "))
Commentvalidate = unescape (request. querystring ("commentvalidate "))
If cstr (session ("getcode") <> commentvalidate then
Echo ("error ")
Response. end ()
Else
SQL = "insert into comment (commentparentid, commentuser, commenttext, commentreply, commentip) values ('" & commentparentid & "', '" & commentuser &"', '"&server.html encode (commenttext) &"', ',' "& getip ()&"')"
Set rs = conn.exe cute (SQL)
Closers ()
Closeconn ()
Echo ("Comments posted successfully .")
End if
End function
%>

%>
The article does not refresh the comment code. If you want ajax to load the comment and send the comment, download it.

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.