greg
greg

Reputation: 25

Saving the click count of multiple buttons to file using AJAX and PHP

I have some code which is working just fine counting the number of clicks of a single button. However I don't know how to let it count multiple buttons for me.

How should I extend my code to count clicks from other buttons?

PHP script:

<?php

$counterFile = 'counter.txt' ;
if (isset($_GET['increase'])) {
    if (($counter = @file_get_contents($counterFile) ) === false ) {
        die('Error : file counter does not exist');
    }
    file_put_contents($counterFile,++$counter) ;
    echo $counter ;
    return false ;
}

if (!$counter = @file_get_contents($counterFile)) {
    if (!$myfile = fopen($counterFile,'w')) {
        die('Unable to create counter file !!') ;
    }
    chmod($counterFile,0644);
    file_put_contents($counterFile,0) ;
}
?>

Javascript:

$('#download1').on('click',function(){
    jQuery('div#counter').html('Loading...') ;
    var ajax = jQuery.ajax({
        method : 'get',
        url : '/test.php', // Link to this page
        data : { 'increase' : '1' }
    }) ;
    ajax.done(function(data){
        jQuery('div#counter').html(data) ;
    }) ;
    ajax.fail(function(data){
        alert('ajax fail : url of ajax request is not reachable') ;
    }) ;
});

Upvotes: 1

Views: 1446

Answers (3)

Peter
Peter

Reputation: 9123

To clear things up I've split my answer into three parts (PHP, JS and HTML) so you can see how it works.

PHP

$counterFile = 'counter.txt';
if (isset($_GET['increase'])) {
    if (($counters = unserialize(@file_get_contents($counterFile))) === false) {
        die('Error : file counter does not exist');
    }
    switch ($_GET['counter']) {
        case 'counter_one':
            $counters['counter_one'] += $_GET['increase'];
            break;
        case 'counter_two':
            $counters['counter_two'] += $_GET['increase'];
            break;
    }
    file_put_contents($counterFile, serialize($counters)) ;
    foreach ($counters as $name => $count) {
        echo $name.": ".$count."<br />";
    }
    exit;
}

if (!$counters = unserialize()@file_get_contents($counterFile)) {
    if (!$myfile = fopen($counterFile,'w')) {
        die('Unable to create counters file');
    }
    chmod($counterFile,0644);
    file_put_contents($counterFile, serialize(array('counter_one' => 0, 'counter_two' => 0)));
}

Javascript

$(document).ready(function ($) {
    $(document).on('click', '.counter', function() {
        var counter = $(this).data('counter');
        jQuery('div#counter').html('Loading...') ;
        var ajax = jQuery.ajax({
            method : 'get',
            url : '/test.php', // Link to this page
            data : { 'increase' : '1', 'counter': counter }
        }) ;
        ajax.done(function(data){
            jQuery('div#counter').html(data);
        });
        ajax.fail(function(data){
            alert('ajax fail : url of ajax request is not reachable') ;
        });
    });
});

HTML

Simply add a data attribute to your buttons.

<button class="counter" data-counter="counter_one">Click me</button>
<button class="counter" data-counter="counter_two">Click me</button>

Upvotes: 1

Cesar Bielich
Cesar Bielich

Reputation: 4945

Your making this way harder than you need too. Just use a DB for this. You are already using AJAX so just call the DB with AJAX and count the clicks that way.

php script counterdownload1.php:

<?
$sql = mysql_query("UPDATE download1 SET counter = counter+1");
?>

ajax:

$('#download1').on('click',function(){
    // blah blah your code
    $.ajax({
        type: "POST",
        url: "counterdownload1.php"
    });
)};

Upvotes: -1

Barmar
Barmar

Reputation: 781078

Send the button ID to the server script, and store an associative array in the file in JSON format.

PHP:

<?php

$counterFile = 'counter.json' ;


if ( isset($_GET['increase'], $_GET['button']) )
{
    $button_name = $_GET['button'];
    if ( ( $counter = @file_get_contents($counterFile) ) === false ) die('Error : file counter does not exist') ;
    $count_array = json_decode($counter, true);
    $count_array[$button_name] = isset($count_array[$button_name]) ? $count_array[$button_name] + 1 : 1;
    file_put_contents($counterFile, json_encode($count_array)) ;
    echo $count_array[$button_name] ;
    return false ;
}

if ( ! $counter = @file_get_contents($counterFile) )
{
    if ( ! $myfile = fopen($counterFile,'w') )
        die('Unable to create counter file !!') ;
    chmod($counterFile,0644);
    file_put_contents($counterFile, json_encode(array())) ;
}

?>

JS:

$('.download').on('click',function(){
    jQuery('div#counter').html('Loading...') ;
    var ajax = jQuery.ajax({
        method : 'get',
        url : '/test.php', // Link to this page
        data : { 'increase' : '1', button: this.id }
    }) ;
    ajax.done(function(data){
        jQuery('div#counter').html(data) ;
    }) ;
    ajax.fail(function(data){
        alert('ajax fail : url of ajax request is not reachable') ;
    }) ;
}) ;

Upvotes: 1

Related Questions