使用AJAX和PHP将多个按钮的点击次数保存到文件

时间:2016-12-22 07:44:28

标签: javascript php ajax

我有一些代码可以很好地计算单个按钮的点击次数。但是,我不知道如何让它为我计算多个按钮。

如何扩展我的代码以计算来自其他按钮的点击次数?

PHP脚本:

<?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') ;
    }) ;
});

3 个答案:

答案 0 :(得分:2)

为了清理问题,我将我的答案分为三个部分(PHP,JS和HTML),以便您了解它是如何工作的。

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

只需在按钮中添加数据属性即可。

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

答案 1 :(得分:2)

将按钮ID发送到服务器脚本,并以JSON格式将关联数组存储在文件中。

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') ;
    }) ;
}) ;

答案 2 :(得分:-1)

你这样做比你需要的更难。只需使用数据库即可。您已经在使用AJAX,因此只需使用AJAX调用数据库并按照这种方式计算点击次数。

php脚本 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"
    });
)};