0

I have a table with a bunch of check boxes, this is not a plug: (found at ), I am trying to figure out how to store each checkbox's value in real time (there is no "Submit" button). I run jQuery through my site and was thinking that PHP/jQuery could do it. I don't need any security with this storage although an IP check to make sure the same user isn't clicking/unclicking the same button 100 times would be nice. All I need it to do is store (I'm thinking a checkbox.log file) how many times each one is checked (use IP to prevent duplicates) than show them sorted in most checked to least checked order. Also I have a few checked to make sure that people realize those are checkboxes(they are jQuery modified), so I would not want those check marks to be counted unless a user actually checks another box(or unchecks one of those two). If you could please give me a pointer as to how this can be accomplished or at least where to begin. I would really prefer to use PHP, with jQuery if needed, but I would really prefer not to use MySQL.

I thinking the log file would look like this (would prefer it to be in Binary(not human readable) if possible):

/* My Checkbox Tallies checkbox.log */
reviews-chk-longer 597
reviews-chk-discrete 256
reviews-chk-natural 375
reviews-chk-thicker 539
reviews-chk-permanent 467
reviews-chk-stamina 390
reviews-chk-harder 310
reviews-chk-vascularity 279
reviews-chk-drive 136
reviews-chk-guarantee 536
reviews-chk-trials 410
reviews-chk-affordable 357

/* IP Logger  How many times that IP checked each box then the actual IP */
0 0 1 1 0 1 1 0 0 1 1 0 192.168.1.1
0 0 70 5 3 4 3 2 0 1 2 0 17.15.45.12
.
.
.
.
.
/* End of File */

I am thinking that to make this simple just stop tallying any marks after 4(a user can check each box 2 times, a few duplicates is okay) and obviously the real file wouldn't have those comments.

so if someone checkmarks the box I assume a javascript needs to run (can php be used on a trigger?) that will ++ the variable or -- the variable if someone unchecked that box.

The other concern is the 2 checked boxes, I don't want it to subtract from the tallies if someone unchecks those boxes for the first time (I guess I can IF...THAN that deal based on how many times they clicked the checked boxes)

Finally it needs to read this file and sort the 12 variables based on most checkmars to least checkmars and display the sorted variables in a separate div. (Although I can do this part on my own)

The first part is a bit more difficult, particularly handling the onClick event to add those values to the file.

Please help me figure out how to do this. Thank you.

Edited by Ezzaral: Snipped url. Not necessary for the question.

1
Contributor
7
Replies
8
Views
7 Years
Discussion Span
Last Post by SerjSagan
0

Just reading through some php documentation, the file will actually look like this(when viewed with a binary viewer):

597 256 375 539 467 390 310 279 136 536 410 357
0 0 1 1 0 1 1 0 0 1 1 0 192.168.1.1
0 0 70 5 3 4 3 2 0 1 2 0 17.15.45.12

So reading the data I was thinking of using $data = file_get_contents($file); because it is safe to use of overly secure hosts. This will be used for writing after a user checks a box $f=file_put_contents($file, $content); Will it be too much on the server? So much file reading/writing? Is this even possible?

0

So looking around some more, to minimize server load with file read/write I would have to do this (just do it one time once the user tries to leave the page:

window.onbeforeunload = function () {
  var checkboxImg = new Image();
  checkboxImg.src = "checkbox.php?reviews-chk-longer="+ reviews-chk-longer.checked +
                    "&reviews-chk-discrete=" + reviews-chk-discrete.checked + 
                    "&"...The rest of the checkboxes...;
}

I know that window.onbeforeunload doesn't work in some rare instances, but as I said earlier these figures don't need to be 100% perfect, I'd be happy with 50% executions :)

Now in my PHP file I can pull a clients IP address with:
function getRealIpAddr()
{
if (!empty($_SERVER)) //check ip from share internet
{
$ip=$_SERVER;
}
elseif (!empty($_SERVER)) //to check ip is pass from proxy
{
$ip=$_SERVER;
}
else
{
$ip=$_SERVER;
}
return $ip;
}

Open my file with $data = file_get_contents($file);
or should I make it easier on myself and just use:

$checkboxes = file('checkbox.log');

(The above file won't be in binary format though...but maybe it is better to go simple sometimes.)

then
1) check to make sure that none of the array elements contain the IP of the current user
2) make sure that there are other checkmarks other than just the 1st 2
3) just add the values that were passed to this php file via the javascript to our array (an unchecked checkbox returns 0, yay!) using:

$reviews-chk-longer=$_GET['reviews-chk-longer'];

4) add the IP address of the current user to a new array element
5) write our array to the file (would it be easier to convert the array into a string then just use $f=file_put_contents($file, $content);?)

And that's it? Am I missing anything?

Edited by SerjSagan: n/a

0

Well, I've implemented everything, but the Javascript is not calling my PHP file...why?
Is it because the
window.onbeforeunload = function () {

or because I am trying to run the php via
checkboxImg.src =

?

0

it seems like window.onbeforeunload isn't running when I leave the page...grr

0

So window.onbeforeunload is working fine...but something to do with the way my url string is structured is the problem. This script does nothing, no alert, no calling fo the .php file...nothing.

<script type="text/javascript">
<!--
window.onbeforeunload = function (){
	var checkboxImg = new Image();
	var checkboxImgUrl = "checkbox.php%3Flonger%3D" + reviews-chk-longer.checked +
                    "%26discrete%3D" + reviews-chk-discrete.checked + 
                    "%26natural%3D" + reviews-chk-natural.checked +
                    "%26thicker%3D" + reviews-chk-thicker.checked +
					"%26permanent%3D" + reviews-chk-permanent.checked +
					"%26stamina%3D" + reviews-chk-stamina.checked +
					"%26harder%3D" + reviews-chk-harder.checked +
					"%26vascularity%3D" + reviews-chk-vascularity.checked +
					"%26drive%3D" + reviews-chk-drive.checked +
					"%26guarantee%3D" + reviews-chk-guarantee.checked +
					"%26trials%3D" + reviews-chk-trials.checked +
					"%26affordable%3D" + reviews-chk-affordable.checked;
	alert(checkboxImgUrl);
	checkboxImg.src = checkboxImgUrl;
}
// -->	
</script>

Where as the same script without the string works just fine, both got an alert and file was run.

<script type="text/javascript">
<!--
window.onbeforeunload = function (){
	var checkboxImg = new Image();
	var checkboxImgUrl = "checkbox.php";
	alert(checkboxImgUrl);
	checkboxImg.src = checkboxImgUrl;
}
// -->	
</script>
0

Wow!!!! Almost 48 hours over 150 views and not one person has given any time of an inkling of help...come on!!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.