×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Storing Click Count Data

Storing Click Count Data

Storing Click Count Data

(OP)
I'm having some trouble storing data for a click counter.  Every time someone clicks on "Good" or "Bad", the number next to it will count this, but the problem is that as of yet the count gets reset to zero whenever the page is reloaded.  The code is as follows:

CODE

<script>
function add_to_goodcount1(){
    var goodcount1=document.getElementById('goodcount1');
    var cstore = document.getElementById('good_counter_score1');

    cstore.value=parseInt(cstore.value)+1;
    goodcount1.innerHTML=cstore.value;
}

function add_to_badcount1(){
    var badcount1=document.getElementById('badcount1');
    var cstore = document.getElementById('bad_counter_score1');

    cstore.value=parseInt(cstore.value)+1;
    badcount1.innerHTML=cstore.value;
}
</script>

<a href="javascript:add_to_goodcount1();">Good </a><input type="hidden" name="good_counter_score1" id="good_counter_score1" value='0'><span id="goodcount1">0</span>
<p>&nbsp;</p>
<a href="javascript:add_to_badcount1();">Bad </a><input type="hidden" name="bad_counter_score1" id="bad_counter_score1" value='0'><span id="badcount1">0</span>

I was wondering if anyone would be able to point me in the best direction as to how to store the data into a text file for both the good and bad count so that when a user returns to the site the count remains the same, or has grown from other users clicking as well.  I'm only just getting used to AJAX and all, so any help would be greatly appreciated.  Thanks!

RE: Storing Click Count Data

Well, this is just Javascript, no AJAX involved, as far as I can see.

The context of the variable is the page, so the data vanishes with it. To persist it, maybe you should think on a cookie or a server-side solution, depending on the persistence level you need

Cheers,
Dian

RE: Storing Click Count Data

Hi

Cookies are not enough because the OP wrote :

Quote (nielse63):

or has grown from other users clicking as well
nielse63, AJAX is about communication. Between client and server. So far you posted only the client-side code and mentioned nothing about the server-side. So tell us about the server-side.

By the way, I am wondering how the JavaScript part could work. You are reading out the current value from the element with id good_counter_score1, then putting the incremented value back into the element with id goodcount1. I feel a gap in the logic there.

Feherke.
http://rootshell.be/~feherke/

RE: Storing Click Count Data

Good point feherke: if you plan to share the counter among several users, you need a common point to store the value.

Btw, there's a hidden field to store the value and a span to show it.

Cheers,
Dian

RE: Storing Click Count Data

Hi

Quote (Dian):

Btw, there's a hidden field to store the value and a span to show it.
Oops. Yes, I saw that. The part I missed is that the incremented value is put back into cstore.value. Sorry, but I find hard to follow that code.
 

Feherke.
http://rootshell.be/~feherke/

RE: Storing Click Count Data

(OP)
thanks for all the help guys.  I am really new to this, and though it is just an experiment i feel like i have to make this work.

are there any tips on how to store the info into a text file and recall it or something that you might be able to give me?  i've tried that with some samples and everything (counters, etc) and it's worked, but i haven't been able to work it into the whole clicking on the link.

any examples would also help.

RE: Storing Click Count Data

You will need some server side technology to make a counter for all users

Cheers,
Dian

RE: Storing Click Count Data

(OP)
I have the server side technology, and access and all, but I don't have the code.  I'm using PHPmyAdmin and MySQL through my hosting service.  My main problem is just storing and recalling the data for all users to see.

RE: Storing Click Count Data

(OP)
Ok, so with the server side technology and everything in place, and the database and table created for all this stuff, it is STILL not working.  The code updates and php are as follows:

CODE

function ajaxFunction(count) {
var xmlhttp;
if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest();
} else if (window.ActiveXObject) {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("POST","goodbad.php",true);
xmlhttp.send(POST);
}

function add_to_goodcount1(){
    var goodcount1=document.getElementById('goodcount1');
    var cstore = document.getElementById('good_counter_score1');

    cstore.value=parseInt(cstore.value)+1;
    goodcount1.innerHTML=cstore.value;
    ajaxFunction(cstore.value);
}

function add_to_badcount1(){
    var badcount1=document.getElementById('badcount1');
    var cstore = document.getElementById('bad_counter_score1');

    cstore.value=parseInt(cstore.value)+1;
    badcount1.innerHTML=cstore.value;
    ajaxFunction(cstore.value);
}


<body>
<a href="javascript:add_to_goodcount1();">Good </a><input type="hidden" name="good_counter_score1" id="good_counter_score1" value='0'><span id="goodcount1">0</span>
<p>&nbsp;</p>
<a href="javascript:add_to_badcount1();">Bad </a><input type="hidden" name="bad_counter_score1" id="bad_counter_score1" value='0'><span id="badcount1">0</span>
</body>

And here's the php file:

CODE

<?php

$count = $_POST['count'];

mysql_connect("localhost", "username", "password");
mysql_select_db("raterest1");

mysql_query("UPDATE count SET count='$count'") or die(mysql_error());

?>

Would anyone know what's going on here, why it's not storing the data?

RE: Storing Click Count Data

CODE

xmlhttp.open("POST","http://hostname:port/goodbad.php",true);


 

Cheers,
Dian

RE: Storing Click Count Data

Hi

The ajaxFunction() does nothing with its count parameter.

CODE --> JavaScript

function ajaxFunction(count)
{
  var xmlhttp;
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  } else return;

  xmlhttp.open("POST","goodbad.php",true);
  xmlhttp.send('count='+count);
}
By the way, the logic is quite wrong.

Feherke.
http://rootshell.be/~feherke/

RE: Storing Click Count Data

Hi

This is how I would do it :

CODE --> JavaScript

function ajaxFunction(direction)
{
  var xmlhttp;
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  } else return;

  xmlhttp.open('GET','goodbad.php?direction='+direction,true);
  xmlhttp.send(null);
}

function add_to_count(direction)
{
  var counthold=document.getElementById((direction==1?'good':'bad')+'count');
  counthold.innerHTML=parseInt(counthold.innerHTML,10)+1;
  ajaxFunction(direction);
}

CODE --> HTML

<a href="javascript:add_to_count(1);">Good</a><span id="goodcount">0</span>
<a href="javascript:add_to_count(-1);">Bad</a><span id="badcount">0</span>

CODE --> goodbad.php

<?php

$direction=$_GET['direction'];

if ($direction!=1 && $direction!=-1) return;
$field=($direction==1?'good':'bad').'count';

mysql_connect("localhost", "username", "password");
mysql_select_db("raterest1");

mysql_query("update count set $field=$field+1") or die(mysql_error());

?>
Warning The above code was not tested

Feherke.
http://rootshell.be/~feherke/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close