Forum Moderators: open

Message Too Old, No Replies

Classic ASP and AJAX

         

VvvJRvvV

4:42 am on Jun 4, 2007 (gmt 0)

10+ Year Member



I have been a long time lurker of this site, there is some really great information here. I would first like to thank you for your efforts. On to my dilemma.

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 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.

Below is a link to the page I am trying to do this on, hopefully, this will give some insight as to what I am doing.

<url removed>

Thank you in advance for your time,
JR

[edited by: encyclo at 1:49 pm (utc) on June 4, 2007]
[edit reason] no URLs please, see terms of service [/edit]

colandy

1:12 pm on Jun 4, 2007 (gmt 0)

10+ Year Member



I've never used access on a website before but here's a suggestion.

I assume the only time the popup appears is when the user clicks on the image. If this is the case why not just have a counter for that image in the database that has 1 added to it wheneveer the popup is opened.

The popup is obviously aware of which pic is being opened and therefore adding 1 to a count for that image shouldn't be an issue, dependant upon how you db is structured.

No need for AJAX.

Trace

2:30 pm on Jun 4, 2007 (gmt 0)

10+ Year Member



Create a page called picture.asp

inside that page, put something like this


<!--#include file="includes/connect.asp"-->
<%
'open database funtion from connect.asp
OpenDatabase Dataconn
Set picRS = Server.CreateObject("ADODB.Recordset")
SQL = "SELECT xImageLocation FROM pictures WHERE id = " & (request("id")*1)
picRS.Open SQL,DataConn

' check to make sure picture is in database
If Not picRS.eof Then
oUrl = picRS(0)
oClics = (picRS(1) + 1)

' update field containing the amount of views
SQLupd = "UPDATE pictures SET xClics = "&oClics&" WHERE id = " & (request("id")*1)
DataConn.Execute ( SQLupd )

Else
response.write "Sorry, there has been a problem displaying this image."
End If

picRS.Close
Set picRS = Nothing
CloseDatabase Dataconn

'redirect to the picture
response.redirect(oUrl)
%>

Then call the page like so: picture.asp?id=12345

VvvJRvvV

2:18 am on Jun 5, 2007 (gmt 0)

10+ Year Member



To clarify a bit more. I am attempting to store the number of clicks on a particular image in the 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.

My thoughts were if I used AJAX, I could update the story page with the total number of clicks and popup the window with the photo in it. I do not want to open a new page nor do I want to refresh the page.

Thanks,
JR

colandy

7:27 am on Jun 5, 2007 (gmt 0)

10+ Year Member



Not being a vbScript man, can u not put the vbScript to add to the db within the popup?

colandy

7:30 am on Jun 5, 2007 (gmt 0)

10+ Year Member



AJAX can update the main page with the new count once the popup has been opened and the count has been added.

VvvJRvvV

7:38 am on Jun 5, 2007 (gmt 0)

10+ Year Member



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 demo, PM me if you would like to see it.

I can not get the code to show up properly on the forum, I must be doing something wrong.

Basically, I used the get object to call on an ASP page that updates the database. I still can not figure out why you have to open the page in a new window to be able to increment the values again.

Some relevant javascript code


function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}

var http = createRequestObject();

function sndReq(imageid) {
http.open('get', 'connect.asp?imageid='+imageid);
http.onreadystatechange = handleResponse;
http.send(null);
}

Thanks,
JR

colandy

10:34 am on Jun 5, 2007 (gmt 0)

10+ Year Member



OK let me get this str8 in my head, on original page you create the AJAX object once the image is clicked, this Object calls the ASP page, adds 1 to the image counter and then back on the original page the popup opens and the image is displayed.

Does the ASP page return the new count..?

If so this can be used to update the original page.

VvvJRvvV

1:26 pm on Jun 5, 2007 (gmt 0)

10+ Year Member



Yes, it does return the new value. Like I said the only problem I see with it is this will only increment each ID once, if you open this window in a new browser, you can increment the values again.

Thanks,
JR

VvvJRvvV

1:17 am on Jun 7, 2007 (gmt 0)

10+ Year Member



I figured it out. It was a simple case of having things in the wrong order.

Thanks to everyone that helped out,
JR

colandy

11:26 am on Jun 7, 2007 (gmt 0)

10+ Year Member



Your site still has an issue.

It adds 1 every other click. Not every time it is clicked.

Try clicking on first image 6 times, it will add 3 to count.