Simple ajax+asp voting system source code

Source: Internet
Author: User

/*
*--------------------------------------------------------------------
*
* Create a XMLHTTP instance
*return object is successfully created to return a XMLHTTP instance, otherwise it returns false
*
*--------------------------------------------------------------------
*/
function Createajax ()
{
var XMLHttp;
Try
{
XMLHttp = new ActiveXObject ("Microsoft.XMLHTTP"); How IE is created
}
catch (E)
{
Try
{
XMLHttp = new XMLHttpRequest (); How to create a browser such as FF
}
catch (E)
{
XMLHttp = false; Creation failed, return false
}
}
return XMLHttp; Return XMLHTTP Instance
}

/*
*-------------------------------------------------------------------------
*
* Function Name: Dig (ID)
* Function: Vote
* Parameter: ID
* 1, ID: The ID of the article
*
*-------------------------------------------------------------------------
*/
function Dig (ID)
{
_xmlhttp = Createajax ();
var url = ' service.asp?id= ' +id+ ' &n= ' +math.random () + '; This adds a parameter n, represented as a random number, to avoid browser caching
if (_xmlhttp)//To determine whether XMLHTTP was created successfully
{
var content = document.getElementById ("Dig" +id);
var dig = document.getElementById (ID); Get the node in the page with ID as article ID
_xmlhttp.open (' Get ', url,true);
_xmlhttp.onreadystatechange=function ()
{
if (_xmlhttp.readystate = 4)//Client completion request
{
if (_xmlhttp.status = = 200)//server completes processing and returns data
{
var responsetext = unescape (_xmlhttp.responsetext); Get the display character for the service
var r=responsetext.split (","); If already voted, the service will display dig, the number of votes, where the split character is used to display errors and reset the Count
if (r[0] = = "Dig")//service returned the error message
{
Alert ("You've already voted!") ");
DIG.INNERHTML=R[1];
}
No articles found
else if (responsetext = "NoData")
{
Alert ("Parameter Error!) ");
}
Else
{
The server returned a success and the number of votes was reset
Dig.innerhtml=responsetext;
Alert ("successful vote");
Convert a vote to a button to view an article
Content.innerhtml= ' <a href= ' shownew.asp?id= ' + id + ' > View </a> ';
}
}
else//Server exception
{
Alert (the server returns an exception!) ");
top.location.href= ' index.asp ';
}
}
else//When the request is not completed prompt information
{
Dig.innerhtml= ' ';
}
}
_xmlhttp.send (NULL); Send a request to the server because it is a GET request that is appended directly to the URL, so the data in parentheses is Null,ie, but FF must add null, or it will fail.
}
else//Creation not successful
{
Alert ("Your browser does not support or does not enable xmlhttp!");
}
}

Above is the XMLHTTP instance's data no flush processing, below is the ASP data processing and returns the data

<!--#include file= "conn.asp"-->
<%
Dim ID
Dim Rs,sql
id = Replace (Trim (Request.QueryString ("id")), "'", ""
If session ("id" &id) <> "Then
Set Rs = Server.CreateObject ("ADODB.") Recordset ")
SQL = "SELECT * from News Where id=" &id
Rs.Open sql,conn,3,3
If rs.eof and Rs.bof Then
Response.Write ("NoData")
Else
Response.Write ("Dig")
Response.Write (",")
Response.Write (Rs ("Dig"))
End If
Else
Set Rs = Server.CreateObject ("ADODB.") Recordset ")
SQL = "SELECT * from News Where id=" &id
Rs.Open sql,conn,3,3
If rs.eof and Rs.bof Then
Response.Write ("NoData")
Else
Dim Dig
Dig =rs ("Dig")
Dig = Dig + 1
Rs ("Dig") = Dig
Rs.update
Rs.close
Set Rs = Nothing
Session ("id" &id) = ID
Response.Write (Dig)
End If
End If
%>

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.