How to tell if a person has clicked on a DIV or not

0

I have an ad and I want to know if the person has already clicked on the ad in the last 2 days.

If it has already clicked, it does not appear, but if it did not click, it still appears.

Something like this:

<!DOCTYPE html>
<?php
  setcookie("USER","sim", time() + 172800); // 86400 = 1 dia, 172800 = 2 dia
?>
<html>
<body>

<?php if (!isset($_COOKIE["USER"])): ?>
  <div class="anuncio">
    ANUNCIO PARA QUEM NÃO CLICOU AINDA!
  </div>
<?php else: ?>
  <div class="">
    OPS, VC JA CLICOU NÃO APARECER ANUNCIO
  </div>
<?php endif; ?>

</body>
</html>

But how do you know if he already clicked within this <div> , that is, if he already clicked on the ad

    
asked by anonymous 14.10.2018 / 00:23

2 answers

1

Use Java script with ajax, to load the page in PHP when you click on the div!

<script type="text/javascript">
 function gravaCookieclique() {
  $.ajax({
   method: "POST",
   url: "gravacookieclique.php",
   data: { gravaCookieclique: "sim"}
  });
 }
</script>

And in PHP put your function with $ _POST

    
16.10.2018 / 03:39
0

A non-PHP and cookie-only solution with JS and LocalStorage:

//Elemento do anuncio
const anuncio = document.querySelector('#anuncio');

//Se houver o anuncio salvo
if (localStorage.anuncio) {
  //Cria um objeto de data com o valor salvo
  let data = new Date(localStorage.anuncio);
  //Adiciona dois dias a data criada
  data.setDate(date.getDate() + 2);

  //Compara a data salva com a data atual
  if (data > new Date()) {
    //Se a data salva + 2 dias for maior que a data atual, esconde o anuncio
    anuncio.style.display = 'none'
  }
}

//Adiciona um ouvinte de evento no elemento do anuncio
anuncio.addEventListener('click', function() {
  //Salva em localStorage a data
  localStorage.anuncio = new Date();
});
    
14.10.2018 / 00:58