I am a total Java noob. I can barely use HTML and PHP. I have a personal website and am using a java button to allow visitors to choose themes. One theme is blues and another is black and white with much larger text.
What I would like to do is count the uses of the BIG button for the dark style CSS. Is there a way to do this?
PS the div id "themebuttons" is just background color and border, and the class "roundedbutton" just changes the shape of the button.
This is the button code:
<div id="themebuttons">THEME<br>
<form>
<input class="roundedbutton" type="button" value=" Blues " onclick="switch_style('blue');return false;">
<br>
<input class="roundedbutton" type="button" value=" B I G " onclick="switch_style('dark');return false;">
</form>
</div>
The buttons call this script "switch_style" which will, well, switch the page stylesheet being used:
<script>
var style_cookie_name = "style" ;
var style_cookie_duration = 30 ;
var style_domain = "domain.com" ;
function switch_style ( css_title )
{
var i, link_tag ;
for (i = 0, link_tag = document.getElementsByTagName("link") ;
i < link_tag.length ; i++ ) {
if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) &&
link_tag[i].title) {
link_tag[i].disabled = true ;
if (link_tag[i].title == css_title) {
link_tag[i].disabled = false ;
}
}
set_cookie( style_cookie_name, css_title,
style_cookie_duration, style_domain );
}
}
function set_style_from_cookie()
{
var css_title = get_cookie( style_cookie_name );
if (css_title.length) {
switch_style( css_title );
}
}
function set_cookie ( cookie_name, cookie_value,
lifespan_in_days, valid_domain )
{
var domain_string = valid_domain ?
("; domain=" + valid_domain) : '' ;
document.cookie = cookie_name +
"=" + encodeURIComponent( cookie_value ) +
"; max-age=" + 60 * 60 *
24 * lifespan_in_days +
"; path=/" + domain_string ;
}
function get_cookie ( cookie_name )
{
var cookie_string = document.cookie ;
if (cookie_string.length != 0) {
var cookie_array = cookie_string.split( '; ' );
for (i = 0 ; i < cookie_array.length ; i++) {
cookie_value = cookie_array[i].match ( cookie_name + '=(.*)' );
if (cookie_value != null) {
return decodeURIComponent ( cookie_value[1] ) ;
}
}
}
return '' ;
}
</script>
Then I call the cookie and load the right CSS on each page in the BODY tag:
<body onload="set_style_from_cookie()";>
On my pages I use a PHP file to count page hits using
<?php include("counter/countername.php"); ?>
where countername is different for each page.
countername.php is this:
<?php
if (file_exists('counter/countername_file.txt'))
{
$fil = fopen('counter/countername_file.txt', 'r');
$dat = fread($fil, filesize('counter/countername_file.txt'));
echo $dat+1;
fclose($fil);
$fil = fopen('counter/countername_file.txt', 'w');
fwrite($fil, $dat+1);
}
else
{
$fil = fopen('counter/countername_file.txt', 'w');
fwrite($fil, 1);
echo '1';
fclose($fil);
}
?>
When I insert a PHP to call and increment in the button code like this:
<input class="button" type="button" value=" Blues " onclick="switch_style('blue');<?php
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/counter/themeblue_file.txt'))
{
$fil = fopen($_SERVER['DOCUMENT_ROOT'].'/counter/themeblue_file.txt', 'r');
$dat = fread($fil, filesize($_SERVER['DOCUMENT_ROOT'].'/counter/themeblue_file.txt'));
echo $dat+1;
fclose($fil);
$fil = fopen($_SERVER['DOCUMENT_ROOT'].'/counter/themeblue_file.txt', 'w');
fwrite($fil, $dat+1);
}
else
{
$fil = fopen($_SERVER['DOCUMENT_ROOT'].'/counter/themeblue_file.txt', 'w');
fwrite($fil, 1);
echo '1';
fclose($fil);
}
?>;return false;">
<br>
<input class="button" type="button" value=" B I G " onclick="switch_style('dark');<?php
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/counter/themedark_file.txt'))
{
$fil = fopen($_SERVER['DOCUMENT_ROOT'].'/counter/themedark_file.txt', 'r');
$dat = fread($fil, filesize($_SERVER['DOCUMENT_ROOT'].'/counter/themedark_file.txt'));
echo $dat+1;
fclose($fil);
$fil = fopen($_SERVER['DOCUMENT_ROOT'].'/counter/themedark_file.txt', 'w');
fwrite($fil, $dat+1);
}
else
{
$fil = fopen($_SERVER['DOCUMENT_ROOT'].'/counter/themedark_file.txt', 'w');
fwrite($fil, 1);
echo '1';
fclose($fil);
}
?>;return false;">
</form>
</div>
It increments both counters on every page load even if no buttons are clicked.
So is there a way to count and increment each time someone uses one or the other of the buttons so I can see which is more popular or if they are even used?