Я заставил это работать! Я использую этот скрипт для загрузки 2 PHP-файлов, чтобы изменить число с +1 на -1.
$(window).on('load', function() {
// async: false will make the AJAX synchronous in case you're using jQuery
$.ajax({
type: 'POST',
url: 'http://localhost/Onload.php',
data: { ajax_data : 22 },
async: false
});
});
$(window).on('unload', function() {
// async: false will make the AJAX synchronous in case you're using jQuery
$.ajax({
type: 'POST',
url: 'http://localhost/Onclose.php',
data: { ajax_data : 22 },
async: false
});
});
Другие 2 файла здесь => Onload.php:
<?php
$myfile = fopen("Counter.txt", "r") or die("Unable to open file!");
$before = fread($myfile,filesize("Counter.txt"));
fclose($myfile);
$myfile = fopen("Counter.txt", "w") or die("Unable to open file!");
$after = $before + 1;
echo "$before : $after";
fwrite($myfile, $after);
fclose($myfile);
?>
Onclose.php:
<?php
$myfile = fopen("Counter.txt", "r") or die("Unable to open file!");
$before = fread($myfile,filesize("Counter.txt"));
fclose($myfile);
$myfile = fopen("Counter.txt", "w") or die("Unable to open file!");
$after = $before - 1;
echo "$before : $after";
fwrite($myfile, $after);
fclose($myfile);
?>
Теперь вам нужно всего лишь создать скрипт для считывания txt-файла и распечатать его в элементе span. Надеюсь, я вам помогу!