M
M
Maxim2016-09-04 13:37:38
PHP
Maxim, 2016-09-04 13:37:38

Customer counter on the site?

There is a static page that has a customer counter. How can I make this number increase when the button is clicked? And when reloading the page, the number did not go astray

9e8e0-clip-7kb.jpg?nocache=1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2016-09-04
@maxnrg33

What exactly is your blockage? If the algorithm, then there are not many options

if (данные счетчика хранятся в БД || файле ) {
   отправляем ajax-запрос || обычный запрос на сервер;
   увеличиваем значение на 1 в хранилище;
} elseif (счетчик для каждого клиента свой && данные необходимо хранить на клиенте) {
   используем localStorage/sessionStorage || cookie;
} else {
   // а тут бы неплохо уточнить, что и как у вас построено
}
If the technical side of the issue, then tell me what does not work

B
Bowen, 2016-09-04
@Bowen

Something like this:

var $btn = $('.btn'),
  localStorageButtons = JSON.parse(localStorage.getItem('buttons')) || {};

if (localStorageButtons) {
  $btn.each(function(i) {
    $btn.text(localStorageButtons[i]);
  });
}

$btn.on('click', function() {
  var v = parseInt($(this).text(), 10),
    i = $(this).index();
  v++;
  localStorageButtons[i] = v;
  $(this).text(v);
  localStorage.setItem('buttons', JSON.stringify(localStorageButtons));
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question