View Full Version : Submit Button Counter / Chart

05-22-2009, 12:36 AM
New here to CodingForums thanks for having me. Hello All!

I read an article on this forum that was about a counter that tracked button clicks. The problem is it's not quite what I was looking for so I'm posting to see if anyone can help on this matter.

What we have is a very simple site that has a drop down box that links to several different forms that are filled out then submitted (to a server that accepts HTTP POSTs). What I'm looking to do is put a small chart on the main page with the drop down that shows how many times a specific form was submitted, in essence tracking the submit button clicks on those forms. To be exact we have six forms. Please correct me if im wrong and there is a better way to accomplish this but that was the only way I could think of. I do not have .asp or .php capabilities on the paticular site so sticking to a javascript would be best.

If you need additional info let me know. Thanks for any help anyone can provide me with. :thumbsup:

Old Pedant
05-22-2009, 12:45 AM
Don't see any way to do this without *some* server-side capabilities.

You can't save site-global data anywhere except on the server. And JavaScript can't save data anywhere, in the general case, except in cookies. And each user has his/her own set of cookies. So you could show "Joe" that he has downloaded 3 files, but there would be no way for "Mae" to know about Joe's download.

ASP, PHP, JSP, CGI, CF, whatever. You need *SOME* server side technology to make this work. Irrelevant to JavaScript.

NOTE: *IF* you have server side capabilities on some *other* server, you could use AJAX to have JS code from HTML pages on this server both get the table and update it on the server-capable server.

05-22-2009, 06:56 PM
If I could manage *some* server side capabilitities what would you suggest?
Thanks BTW for the response.

Old Pedant
05-22-2009, 10:57 PM
Really doesn't matter. Any of the common technologies are more than adequate for this.

You should probably use a simple database, unless you are talking about only a handful of people (in which case you could get away with storing the data in a text file).

You aren't talking about more than a couple of dozen lines of code, in JSP/PHP/ASP, along with a simple database (say Access or MySQL or even a ".csv" file).

05-23-2009, 12:24 AM
Correct very small. A txt file would be sufficient. This site is just a portal for submitting those forms I spoke of. I would probably log the values of the counter at the end of each month and start over.

Could you help me with a generalization of how to do it say using .asp to get me started? Once I figure out how to get started I "think" I can get going on it. I just really don't have a clue where to start. Or a basic concept of how to get the onclick to log into a txt file then to display it somewhere on the main page.

Old Pedant
05-23-2009, 01:02 AM
If using ASP, it's about as easy to use an Access DB as it is to use a file, but okay...

We will say that the file format is this:

name : count
with each person on a separate line.

So the file might look like:

And anybody who has not submitted a form doesn't even appear in the file.

We will put the file into a directory named "data" that is under the root directory. We'll put the code in the root directory. We will assume that you will invoke the page by hitting a URL of the form http://www.yoursite.com/countDownloads.asp?name=John+Doe


name = Request("name")
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set infile = FSO.OpenTextFile( Server.MapPath("data/downloads.txt") )
lines = infile.ReadAll ' reads entire text file into memory

lines = Split( lines, vbNewLine ) ' break text into an array of lines
done = False
For i = 0 To UBound(lines)
temp = Split( lines(i), ":" )
If LCase(name) = LCase(temp(0)) Then
' aha! this person already in the file...change his/her count:
temp(1) = CINT( temp(1) ) + 1
' put the line back in place:
lines(i) = Join( temp, ":" )
done = True
Exit For ' and we are done
End If
If Not done Then
' that name not found in the file, so add a line:
ReDim Preserve lines( UBound(lines) + 1 ) ' bump size by 1
lines( UBound(lines) ) = name & ":1" ' give this person one download
End If
' true says overwrite existing file:
Set outfile = FSO.CreateTextFile( Server.MapPath("data/downloads.txt"), True )
outfile.Write Join( lines, vbNewLine ) ' write out all the lines
outfile.WriteLine "" ' with a terminating line break

Yeah, would actually be slightly shorter to use a database.

Old Pedant
05-23-2009, 01:12 AM
Just for grins, here it is using an Access DB, also in the "data" directory:

name = Trim(Request("name"))
If name = "" Then Response.End ' no name? do nothing
name = Replace( name, "'", "''" )

Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath("./data/downloads.mdb")

SQL = "UPDATE userDownloads SET count=count + 1 WHERE name='" & name & "'"
howmany = -1
conn.Execute SQL, howmany
If howmany <= 0 Then
SQL = "INSERT INTO userDownloads (name,count) VALUES('" & name & "',1)"
conn.Execute SQL
End IF

05-23-2009, 03:38 PM
Im going to sit down this weekend and play around with that. Thanks!