Avatar of saabStory
saabStoryFlag for United States of America

asked on 

Use Javascript to increment a text file counter with an onClick event?

I know Javascript is client-side and I need a server-side component to update any file, but I can't find anything that will do what  I need that is not written in .php.  I'd love to use a .php solution but don't have it installed on our server (yet).

What I have is a series of e-mail messages for people to use to send to clients.  The click on the link and the message opens, they add their clients name and send.  I need  a counter or another method that will increment every time one of the links is clicked on to determine how many hits each link is getting.  Ultimately, I'll need 3 counters but if I can get one, I'll be able to get the rest going.

Thanks much in advance.

ASPJavaScriptASP.NET

Avatar of undefined
Last Comment
hielo
Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image

the following solution will only work if your link opens up an email client and doesnt take the user away from teh screen as teh following javascript doesnt support sessions....

if you wnat somethign liek that let me know
<script>
var countEmail = 0;
 
function incrementCounter(){
  count += 1;
 alert(countEmail);
}
 
</script>
 
//while creating teh links do this
 
<a herf='your link' onclick='incrementCounter()'>Link here</a>

Open in new window

Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

What is herf :)
Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image

href.... my mistake....
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

What do you want to do with the counter and where do you want it stored?
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

I prefer herf, reads much better
Avatar of saabStory
saabStory
Flag of United States of America image

ASKER

Sorry if I wasn't clear -- while the alert will work fine, what I really need is to be able to write that count to a text file.  I don't want/need the user to see the count but to be able to go in at a later date and see who had downloaded what.  I'd prefer the counter to be local but am not too picky at this point.
Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image



<script>
var countEmail = 0;
 
function incrementCounter(){
  countEmail += 1;
 WriteToFile(countEmail );
}
 
function WriteToFile(passCount) {
    
    set fso = CreateObject("Scripting.FileSystemObject");  
    set s = fso.CreateTextFile("/test.txt", True);
    s.writeline(passCount);
    s.Close();
 }
 
</script>
 
//while creating teh links do this
 
<a herf='your link' onclick='incrementCounter()'>Link here</a>

Open in new window

Avatar of saabStory
saabStory
Flag of United States of America image

ASKER

Getting an error  - expecting ';' on line 15 but that the asp function - still looking
Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image

try this
<script>
var countEmail = 0;
 
function incrementCounter(){
  countEmail += 1;
 WriteToFile(countEmail );
}
 
function WriteToFile(passCount) {
    
    var fso = CreateObject("Scripting.FileSystemObject");  
    var s = fso.CreateTextFile("E:\test.txt", True);
    s.writeline(passCount);
    s.Close();
 }
 
</script>
 
//while creating teh links do this
 
<a herf='your link' onclick='incrementCounter()'>Link here</a>

Open in new window

Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image

Actually try this... this is what I tested and worked......
<script>
var countEmail = 0;
 
function incrementCounter(){
  countEmail += 1;
  WriteToFile(countEmail );
}
 
function WriteToFile(passCount) {
   var fso = new ActiveXObject("Scripting.FileSystemObject");
	var s = fso.CreateTextFile("test.txt", true);
	s.WriteLine(passCount);
	s.Close();
 }
 
</script>
 
//while creating teh links do this
 
<a herf='your link' onclick='incrementCounter()'>Link here</a>

Open in new window

Avatar of saabStory
saabStory
Flag of United States of America image

ASKER

That cleared but for some reason, now I'm getting an error saying that CreateObject is not defined.  Don't understand that at all.  Will check permisions on the server.
Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image

Dont use that one... Us ethe second code snippet... it will work...
Avatar of saabStory
saabStory
Flag of United States of America image

ASKER

Sorry - missed that while on remote viewer.  Everything works but the counter increment -- Sorry .  Is the createTextFile re-creating the file each time?
Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image

It will if you keep on closinfg teh window....

DO this to test....

Clock on the link and test teh text file without closing the browser....

again click on teh same link..... now you will see that teh counter chenged to 2.....

If you move on to different stuff what we need is somethign that will read the file and then increment....

Are you lookign for somethign liek that... whcih reads the fiel first for counter and tehn increments it.??
Avatar of hielo
hielo
Flag of Wallis and Futuna image

@viviana:
I believe you missed the most important phrase of the problem description:
"I need a server-side component to update any file"
Avatar of hielo
hielo
Flag of Wallis and Futuna image

@saabStory: What you need may be better off implemented with a db. Do you have MS Access?
Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image

you can do teh same in PHP.....

Instad of using javascript..use PHP
Avatar of saabStory
saabStory
Flag of United States of America image

ASKER

vivianaranha - Can't use php - wish I could but we're new to this box and these are diehard MS folks.  The first time I mentioned php, they got all upset because they had a whole company of .net programmers and nobody knew .php (sigh...)
I guess do need something that will read the existing file and increment.  Since I'll ultimately have three links I need to monitor, that would be best.  
Heilo - I can do access or sql if needed.
Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image

If you can use db, why dont you keep a table for each user. So for every click you can update the database field.
Avatar of saabStory
saabStory
Flag of United States of America image

ASKER

Sorry - didn't know you could - thought that the text file would be easier.  How would you get to a db?
ASKER CERTIFIED SOLUTION
Avatar of Vivian Aranha
Vivian Aranha
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of saabStory
saabStory
Flag of United States of America image

ASKER

Okay, Will do...
Avatar of hielo
hielo
Flag of Wallis and Futuna image

see attached comments:
DB:	tracker.mdb
Table Name: "Hits"
Fields:	id			Autonumber					'unique identifier
		total		Number		DefaultValue: 0	'tracks the number of hits
		page			Text							'descriptor for what you are actually tracking
 
Sample Initial Data:
id	total 	page
1	0		product1.asp
2	0		summary.asp
		
		
http://www.yoursite.com/tracker.asp
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Mode=3
Conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("tracker.mdb")
qry="UPDATE [hits] SET [total]=[total]+1 WHERE [hits].[id]=" & Request("id")
Conn.Close
Set Conn=nothing
If "" <> Trim(Request("redirectTo")) Then
	Response.Redirect Trim(Request("redirectTo"))
End If
%>
 
sampleUse.asp
<a href="http://www.yoursite.com/tracker.asp?id=1&redirectTo=product1.asp">Product 1</a>
<a href="http://www.yoursite.com/tracker.asp?id=2&redirectTo=summary.asp">Summary</a>

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

actually, on my previous post, BEFORE you do:
Conn.Close

you need to execute the query:
qry="UPDATE [hits] SET [total]=[total]+1 WHERE [hits].[id]=" & Request("id")
Conn.Execute query
Conn.Close
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo