AJAX to open ASP not working


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: AJAX to open ASP not working

Threaded View

  1. #1
    Join Date
    Mar 2009
    Posts
    1

    Question 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):
    
    <&#37;
    '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
    Last edited by Hack; 03-24-2009 at 01:51 PM. Reason: Added Code Tags

Similar Threads

  1. Using AJAX with an ASP Variable
    By dcampos in forum AJAX
    Replies: 0
    Last Post: 10-02-2007, 12:50 PM
  2. Open File Dialog and Database Location Problem
    By anthony1709 in forum .NET
    Replies: 2
    Last Post: 04-03-2007, 09:27 AM
  3. Replies: 2
    Last Post: 02-16-2007, 02:13 PM
  4. Replies: 0
    Last Post: 02-22-2002, 07:44 AM
  5. Re: Help! ASP files open in Photoshop!
    By Joseph Buelna in forum ASP.NET
    Replies: 1
    Last Post: 01-05-2001, 12:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center