AJAX to open ASP not working
Hi,
I'm making a site where people can make posts, and I have a rating system in ASP. I want it to be that when someone clicks on an image (thumbs_up_green.jpg), a back-end ASP script writes to an Access database to assign a value of "1" to the correct "QuoteID." The ASP code (rategood.asp) works on its own, but for some reason the AJAX isn't firing correctly and won't open it. Here are the three sections that are relevant:
The AJAX code:
Code:
<script type ="text/javascript">
<!--
var xmlHttp
function RateItGood(str)
{
if (str.length==0)
{
document.getElementById("MessageRated").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="rategood.asp";
url=url+"?q="+str;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("MessageRated").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
//-->
</script>
The form code (written in ASP with html) that should open the AJAX (the html shows the page correctly, so the only problem with this could be with the onclick property):
<%
'declare your variables
Dim connection, conn, recordset, rs
Dim sSQL, sConnString, sConnString6, sSQL6, sSQL7, sSQL8
Dim NumGood, NumBad, Good, Bad, QuoteID
Dim Rating, Cookie, cookieRated, VisitorIP
Dim RateMessage
'declare SQL statement that will query the database
sSQL="SELECT * FROM MatchupRatings ORDER BY QuoteID Desc"
sSQL8 = "SELECT COUNT(*) FROM Rating WHERE VisitorIP='" & visitorIP & "' AND QuoteID ='"
& QuoteID & "'"
'create an ADO connection and recordset object
Set connection = Server.CreateObject("ADODB.connection")
Set conn = Server.CreateObject("ADODB.connection")
Set recordset = Server.CreateObject("ADODB.Recordset")
Set rs = Server.CreateObject("ADODB.Recordset")
'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("access_db/SchoolsDatabase.mdb")
sConnString6="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("access_db/SchoolsDatabase.mdb")
'Open the connection to the database
Connection.Open sConnString
conn.Open(sConnString6)
rs.ActiveConnection = conn
'Open the recordset object, executing the SQL
Recordset.Open sSQL, Connection
'Looping through the records until the end of the records
Do while Not recordset.eof
Response.Write "<h3>" & "Overheard at " & recordset("School") & "</h3>"
Response.Write "<i>Where: " & recordset("Location") & "<br>"
Response.Write "When: " & recordset("Timing") & "</i><br>"
Response.Write "<br>"
Response.Write Replace(recordset("Quote"), vbCrLf, "<br/>") & "<br>"
Response.Write "<br><br>"
Response.Write "<font size=1>" & "Submitted by " & recordset("FirstName") & " on " &
recordset("DateStamp") & " at " & recordset("TimeStamp") & "<br> <form name=RateGood
method=post> <input type=image src=thumbs_up_green.jpg id=RateGood onclick=RateItGood(" &
recordset("QuoteID") &")>" & recordset("NumberGood") & "</button> </form></font>"
Response.Write "<br>"
Response.Write "<br>"
Response.Write "<div id=MessageRated" & recordset("QuoteID") &"></div>"
Response.Write "<hr />"
'move on to the next record
'470ish
recordset.MoveNext
loop
'Now close the recordset and the connection object
recordset.Close
Set recordset = Nothing
connection.Close
Set connection = Nothing
%>
And the ASP code at rategood.asp (like I said, works correctly on its own. Also there are some extra variables defined here, so don't be thrown off by those):
<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Rate to database</title>
</head>
<body>
<%
dim conn
dim rs
dim QuoteID
dim visitorIP
dim Rating
dim Cookie
dim cookieRated
dim sSQL7
dim sSQL8
dim sSQL9
dim sConnString
dim RatingMessage
QuoteID=Replace(request.querystring("q"),"/","")
'QuoteID = Replace(Request.Form("QuoteID"),"/","")
Rating = Request.Form("Rating")
VisitorIP = Request.ServerVariables("REMOTE_ADDR")
Cookie = Request.Cookies("rate_" & QuoteID)
'Next up we have our database connection and recordset:
'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("access_db/SchoolsDatabase.mdb")
'create an ADO connection object
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
'Open the connection to the database
conn.Open(sConnString)
rs.ActiveConnection = conn
'If the cookie variable contains a value then the user has already rated this article. We
use a variable called 'cookieRated to determine whether or not this user has already
rated the selected article:
if Cookie = "" then
cookieRated = false
else
cookieRated = true
end if
'We now want to check the ratings table to make sure that this user hasn't already rated
the selected CD, so we use a 'select query, like this:
sSQL8 = "SELECT COUNT(*) FROM Rating WHERE VisitorIP='" & visitorIP & "' AND QuoteID ='"
& QuoteID & "'"
rs.Open sSQL8
'As you can see, we ask for the number of rows where the ip field matches the IP address
of the visitor and where the 'cdId field matches the ID of the CD that is being rated.
'If no records are returned then we check whether or not this user has voted via a
cookie. If they haven?t then we 'add their vote, like this:
sSQL7 = "INSERT INTO Rating(QuoteID, VisitorIP, NumGood, NumBad) VALUES ('" & QuoteID &
"' , '" & VisitorIP & "', '1', '0')"
if rs.Fields(0).Value = 0 then
if cookieRated = false then
'Visitor hasn't rated yet, let's add it
conn.Execute (sSQL7)
Response.Cookies("rate_" & QuoteId) = true
Response.Cookies("rate_" & QuoteId).expires = Date() + 30
RatingMessage = "Thanks!"
else
'Visitor has already rated this article
RatingMessage = "Already Rated!"
end if
else
'Visitor has already rated this article
RatingMessage = "Already Rated!"
end if
Response.Write RatingMessage
'Done. Close the connection object
conn.Close
Set conn = Nothing
%>
</body>
</html>
Any help would be appreciated. I'm pretty new at this. Thanks!
-Andy