Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Counting a button use

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Counting a button use

    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?
    Last edited by Smokr; April 30th, 2022 at 06:52 PM.

Similar Threads

  1. Replies: 3
    Last Post: September 16th, 2014, 01:36 PM
  2. counting mouse button clicks
    By bluewhale in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 25th, 2014, 08:17 AM
  3. [SOLVED] Getting a calculate button to work, and clear button to reset everything.
    By Ancharius in forum AWT / Java Swing
    Replies: 3
    Last Post: April 3rd, 2014, 05:20 PM
  4. Counting # of 7's in an integer
    By greystreet34 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2012, 12:41 PM