By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,621 Members | 1,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,621 IT Pros & Developers. It's quick & easy.

Classic ASP and AJAX

P: 2
I am creating an image gallery in classic asp using a access database. I display the thumbnails and story on the page. When a user clicks on the image, I popup the full image. Popup is necessary to help to combat people taking the images. I am trying to count the number of views or clicks each photo has, I am storing this in an Access database.

I can't or don't know how to have the onclick event fire both the javascript to popup the window and the vbscript to write to the database.

I believe AJAX or XMLHttpRequest is the proper way to do this, I truly do not want a complete page refresh and then the popup. The problem is, I have no clue as to how to do this.


Thank you in advance for your time,
JR
Jun 5 '07 #1
Share this Question
Share on Google+
1 Reply

P: 2
I think I have this figured out. However, I do have a problem. This will only increment each ID once, if you open this window in a new browser, you can increment the values again. I have only tested in IE6. There is a link to a demo at the bottom.

testit.asp
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2.  
  3. <HTML>
  4. <HEAD>
  5. <TITLE>AJAX Test</TITLE>
  6. <style>
  7. body {
  8.  font-family: Verdana;
  9.  font-size: 9pt;
  10. }
  11.  
  12. h3 {
  13. font-family: verdana;
  14. text-align: center;
  15. }
  16.  
  17. a {
  18.   color: #FF0033;
  19. }
  20.  
  21. a:hover {
  22.   color: #000;
  23. }
  24. </style>
  25. <script>
  26. function createRequestObject() {
  27.     var ro;
  28.     var browser = navigator.appName;
  29.     if(browser == "Microsoft Internet Explorer"){
  30.         ro = new ActiveXObject("Microsoft.XMLHTTP");
  31.     }else{
  32.         ro = new XMLHttpRequest();
  33.     }
  34.     return ro;
  35. }
  36.  
  37. var http = createRequestObject();
  38.  
  39. function sndReq(imageid) {
  40.     http.open('get', 'connect.asp?imageid='+imageid);
  41.     http.onreadystatechange = handleResponse;
  42.     http.send(null);
  43. }
  44.  
  45. function handleResponse() {
  46.     if(http.readyState == 4){
  47.         var response = http.responseText;
  48.         var update = new Array();
  49.  
  50.         if(response.indexOf('|' != -1)) {
  51.             update = response.split('|');
  52.             document.getElementById("image_id").innerHTML = "You clicked to increment Image ID : " + update[0];
  53.             document.getElementById("num").innerHTML = "Incremented Number of Clicks : " + update[1];
  54.         }
  55.     }
  56. }
  57. </script>
  58. </HEAD>
  59.  
  60. <BODY>
  61. This is the original pull from the database.
  62. <br><br>
  63. <%
  64.  
  65. set conn = server.createobject("adodb.connection")
  66. DSNtemp="DRIVER={Microsoft Access Driver (*.mdb)}; "
  67. DSNtemp=dsntemp & "DBQ=" & server.mappath("cgi-bin/test.mdb")
  68. conn.Open DSNtemp
  69. sqlstmt = "SELECT * from Table1 Order by ImageID" 
  70. Set rs = Server.CreateObject("ADODB.Recordset")
  71. rs.Open sqlstmt, conn, 3, 3
  72. Do While Not rs.EOF    
  73. imgid = rs("ImageID")
  74. click = rs("num")
  75. response.write ("Image ID : " & imgid)
  76. response.write ("<br>")
  77. response.write ("Number of clicks : " & click)
  78. response.write ("<br><br>")
  79. rs.movenext
  80. loop
  81. rs.close
  82. set rs = nothing
  83. conn.close
  84. set conn = nothing
  85. %>
  86. <br>
  87. Click on one of the links to increment the count.
  88. <br><br>
  89. <a onClick="javascript:sndReq('1');" onmouseover="this.style.cursor='pointer'">Click to Increment 1</a>
  90. <br><br>
  91. <a onClick="javascript:sndReq('2');" onmouseover="this.style.cursor='pointer'">Click to Increment 2</a>
  92. <br><br><br>
  93. <div ID="image_id">The image ID you clicked will show here once you click the increment.</div>
  94. <br>
  95. <div ID="num">The new data will show here once you click the increment.</div>
  96. <br><br><br>
  97. There is something going on here that I can not figure out.
  98. <br>
  99. This will only increment each ID once, if you open this window in a new browser, you can increment the values again.
  100. </BODY>
  101. </HTML>
  102.  
  103.  
connect.asp
Expand|Select|Wrap|Line Numbers
  1. <%
  2. Set theID = Request.QueryString("imageid")
  3.  
  4. set conn = server.createobject("adodb.connection")
  5. DSNtemp="DRIVER={Microsoft Access Driver (*.mdb)}; "
  6. DSNtemp=dsntemp & "DBQ=" & server.mappath("cgi-bin/test.mdb")
  7. conn.Open DSNtemp
  8. sqlstmt = "SELECT * from Table1 WHERE ImageID=" & theID
  9. Set rs = Server.CreateObject("ADODB.Recordset")
  10. rs.Open sqlstmt, conn, 3, 3
  11. num = rs("num")
  12. numC = num + 1
  13. rs.Fields("num") = numC
  14. rs.update
  15. response.write (theID & "|" & numC)
  16. rs.close
  17. set rs = nothing
  18. conn.close
  19. set conn = nothing
  20.  
  21. %>
  22.  
I am still trying to work out why you would have to reload the page to increment either count more than once.

A small demo

Thanks for taking a look at this,
JR
Jun 5 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.